471,056 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,056 software developers and data experts.

Trying to get contents of an ifstream AND find file size at same time

Hello

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;

}

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?

Angus
May 23 '07 #1
2 2456
On 2007-05-23 17:06, Angus wrote:
Hello

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;

}

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?
The easiest way would be to get the size first and read the content
later. The problem you are having is that EOF is a flag set and all the
seek()ing in the world wont change that, you first have to clear the
flag, do this by calling the clear() method.

--
Erik Wikström
May 23 '07 #2
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
May 23 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Foxy Kav | last post: by
1 post views Thread by inkapyrite | last post: by
10 posts views Thread by sam | last post: by
33 posts views Thread by Jason Heyes | last post: by
7 posts views Thread by Hamburgpear | last post: by
3 posts views Thread by phwashington | last post: by
14 posts views Thread by jehugaleahsa | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.