Angus <no****@gmail.comwrote:
Here is my code
std::ifstream myfile;
std::string line;
long begin,end;
myfile.open("c:\\IPlog.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
std::getline (myfile,line);
std::cout << line << std::endl;
}
The condition for the while loop is almost certainly not what you need.
eof() is only set *after* a read has failed. Your loop will probably
output the last line twice. See:
http://www.parashift.com/c++-faq-lit....html#faq-15.5
You want to use the return value of getline() as your loop condition,
like:
while (std::getline(myfile, line)) {
std::cout << line << std::endl;
}
myfile.seekg(0);
begin = myfile.tellg();
myfile.seekg (0, std::ios::end);
end = myfile.tellg();
std::cout << "size is: " << (end-begin) << " bytes.\n";
myfile.close();
}
I only want to open the file once and get the file size then get the
contents.
Trouble is by getting to eof with extracting file contents how do I then get
back to the beginning of the file. I thought myfile.seekg(0); did that?
Once you've hit EOF, you need to clear() the ifstream before you can
really do much else with it.
Also, note that there is no reliable, portable way to get the filesize
in Standard C++. However, please see the thread I started last week
regarding a file read progress indicator, maybe it will give you some
ideas:
http://groups.google.com/group/comp....8ed63cab395e2/
--
Marcus Kwok
Replace 'invalid' with 'net' to reply