![]() |
Check if program finished to read a whole file
The condition at line 31 is added to check if the program finished to
read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using namespace std; int read(string filename, int last_pos) { ifstream file; int size = 0; //read continue after the last read position static int offset = last_pos + 1; file.open(filename.c_str(), ios::in); if(!file) { cerr << "failed to open file: " << filename << endl; return -1; } file.seekg(0, ios::end); size = file.tellg(); file.seekg(offset, ios::beg); string line; while (getline(file, line)) { //... offset = file.tellg(); } //check if it finished to read the whole file if (offset != size) // line 31 { file.clear(); read(filename, offset); } return 0; } |
Re: Check if program finished to read a whole file
lovecreatesbea...@gmail.com wrote:
> The condition at line 31 is added to check if the program finished to > read the whole file. Is it needed and correct? Thank you. > > #include <fstream> > #include <iostream> > #include <string> > using namespace std; > > int read(string filename, int last_pos) Consider passing 'filename' string by a reference to const. > { > ifstream file; > int size = 0; > //read continue after the last read position > static int offset = last_pos + 1; > > file.open(filename.c_str(), ios::in); > if(!file) > { > cerr << "failed to open file: " << filename << endl; > return -1; > } > > file.seekg(0, ios::end); > size = file.tellg(); > file.seekg(offset, ios::beg); > string line; > while (getline(file, line)) > { > //... > offset = file.tellg(); > } > > //check if it finished to read the whole file > if (offset != size) // line 31 > { > file.clear(); > read(filename, offset); Here you seem to be recursing. That means the file is reopened in that function. I don't think it's a good idea. > } > > return 0; > } When 'getline' attempts to read beyond the last byte in the file, the 'file.eof()' will return true. Perhaps you should use that instead of checking the position... What book are you reading on C++ I/O? V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
Re: Check if program finished to read a whole file
Victor Bazarov wrote: > lovecreatesbea...@gmail.com wrote: > > The condition at line 31 is added to check if the program finished to > > read the whole file. Is it needed and correct? Thank you. > > > > #include <fstream> > > #include <iostream> > > #include <string> > > using namespace std; > > > > int read(string filename, int last_pos) > > Consider passing 'filename' string by a reference to const. > > > { > > ifstream file; > > int size = 0; > > //read continue after the last read position > > static int offset = last_pos + 1; > > > > file.open(filename.c_str(), ios::in); > > if(!file) > > { > > cerr << "failed to open file: " << filename << endl; > > return -1; > > } > > > > file.seekg(0, ios::end); > > size = file.tellg(); > > file.seekg(offset, ios::beg); > > string line; > > while (getline(file, line)) > > { > > //... > > offset = file.tellg(); > > } > > > > //check if it finished to read the whole file > > if (offset != size) // line 31 > > { > > file.clear(); > > read(filename, offset); > > Here you seem to be recursing. That means the file is reopened > in that function. I don't think it's a good idea. Thank you. Does a close() call solve this problem as shown below? if (offset != size) // line 31 { file.clear(); file.close(); //close the file. try to re-read the rest again. read(filename, offset); ... > > > } > > > > return 0; > > } > > When 'getline' attempts to read beyond the last byte in the file, > the 'file.eof()' will return true. Perhaps you should use that > instead of checking the position... What book are you reading on > C++ I/O? Yes, I read <The C++ Programming Language, special ed>, <C++ Primer, 4th>, <The C++ Standard Library by Nicolai M. Josuttis> and other books. |
Re: Check if program finished to read a whole file
Victor Bazarov wrote: > When 'getline' attempts to read beyond the last byte in the file, > the 'file.eof()' will return true. Perhaps you should use that > instead of checking the position. I will try to use the eof utility, thank you. |
Re: Check if program finished to read a whole file
Victor Bazarov wrote:
> When 'getline' attempts to read beyond the last byte in the file, > the 'file.eof()' will return true. Perhaps you should use that > instead of checking the position. But one can't know whether getline() fails before reaching the end of the file. Comparing the last read position and the file size tells a file has been read tiil the end or not, right? |
Re: Check if program finished to read a whole file
On 2007-01-23 18:12, lovecreatesbea...@gmail.com wrote:
> Victor Bazarov wrote: >> When 'getline' attempts to read beyond the last byte in the file, >> the 'file.eof()' will return true. Perhaps you should use that >> instead of checking the position. > > But one can't know whether getline() fails before reaching the end of > the file. Comparing the last read position and the file size tells a > file has been read tiil the end or not, right? If readline fails (the loop terminates) it can only be of two reasons, either EOF is reached (in which case eof() == true) or because of a failure (in which case eof() == false, but fail() == true) so it's enough to check eof(). -- Erik Wikström |
| All times are GMT. The time now is 10:19 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.