473,739 Members | 7,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2601
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.c omwrote:
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(m yfile, 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
4283
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 function called readline() because I would also like to do some processing on the line each time (ignoring comments and so on). I have:
6
5649
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 that array back into another file. I've tried, but i can only read the data and place it on the screen, i cant get it into an array. Any help would be helpful.
1
6252
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. Only when the buffer becomes full does it resume execution. Here's my test code for reading from a pipe: //(compiled with g++ -std=c++98) //--------------------------------------------- #include <iostream>
10
18003
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
2887
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 write_file(std::string name, const std::string &s); void find_replace(std::string &s, std::string first, std::string second); bool find_replace_file(std::string name, std::string first, std::string second) {
7
5629
by: Hamburgpear | last post by:
Dear All, Is it possible to reset the value of xxx.peek() after it reachs EOF ? Regards HP
3
3237
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 integer it returns the 8bit integer values. I can convert these to hex and they match up with what I am seeing in the data file. I guess what I need to do somehow buffer these inputs and then read them as floats, but I am not sure how to do...
14
2244
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 incorporate threads so that I can download N files separately. The obvious solution is a thread pool. However, I need to make sure that I download the files Async - so I can get percentage and status information to my interface.
2
2917
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 time. Below is a working script that I used. Let's say after the file is uploaded to the server and a record created with the file contents above to the MS Access (using the script below). If I want to UPDATE that record let's say, change the...
0
9337
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9209
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8215
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6754
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3280
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.