473,396 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2585
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: csvka | last post by:
Hello, I wonder if I could pick your brains. I'm beginning to learn about C++. I have opened a file in my program and I want to read lines from it. I would like this to be done in a separate...
6
by: Foxy Kav | last post by:
Hi, another question from me again, i was just wondering if anyone could give me a quick example of reading data from a file then placing the data into an array for some manipulation then reading...
1
by: inkapyrite | last post by:
Hi all. I'm using ifstream to read from a named pipe but i've encountered an annoying problem. For some reason, the program blocks on reading an ifstream's internal buffer that's only half-filled....
10
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
33
by: Jason Heyes | last post by:
I would like to modify the contents of a file, replacing all occurances of one string with another. I wrote these functions: bool read_file(std::string name, std::string &s); bool...
7
by: Hamburgpear | last post by:
Dear All, Is it possible to reset the value of xxx.peek() after it reachs EOF ? Regards HP
3
by: phwashington | last post by:
I am new to C++ and have a data file I want to read, which was stored in binary. I have looked at the data with a hex editor and it appears to be correct. Whenever I try to read it though as an...
14
by: jehugaleahsa | last post by:
I have a rather complex need. I have a class that parses web pages and extracts all relevant file addresses. It allows me to download every pdf on a web page, for instance. I would like to...
2
by: hotflash | last post by:
Hi Master CroCrew, I found a good PURE ASP that will allow you to upload 10MB file to the server and the file contents such as Network, Author, Title, etc... will insert to MS Access at the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.