Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,617 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 984 people online right now. Registration is fast and FREE... Join Now!




How to read the whole TXT file into one string?

 
Reply to this topicStart new topic

How to read the whole TXT file into one string?

crammer008
post 28 Mar, 2006 - 07:51 PM
Post #1


New D.I.C Head

*
Joined: 9 Feb, 2006
Posts: 14


My Contributions


Dear all,

How to read the whole TXT file into one string?

The TXT file is a multi-line file and its size is not known.

Looking forward to your guidance!
Thanks a lot

Crammer008
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 28 Mar, 2006 - 09:22 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


How can the size not be known? Is it an open text file that is being written to while it is being read from. Here is a link to some common filke i/o manipulation:
http://www.cplusplus.com/doc/tutorial/files.html
As discussed on that page, you can obtain the file size like so:
CODE

#include <iostream>
#include <fstream>
using namespace std;

int main () {
 long begin,end;
 ifstream myfile ("example.txt");
 begin = myfile.tellg();
 myfile.seekg (0, ios::end);
 end = myfile.tellg();
 myfile.close();
 cout << "size is: " << (end-begin) << " bytes.\n";
 return 0;
}

That determined size can then be used to read the file to a single string object.
User is offlineProfile CardPM

Go to the top of the page

crammer008
post 28 Mar, 2006 - 11:25 PM
Post #3


New D.I.C Head

*
Joined: 9 Feb, 2006
Posts: 14


My Contributions


Thank you very much!

You code helps to get the size of the file:)

My question is how to read the whole file into a string.
Every time, the file will be produced with different size. This is the reason I said the size is unknown.

Thank you:)
Crammer008
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 29 Mar, 2006 - 03:53 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


You would check the file size everytime you need to read it. As for reading it to the string, you can do it several ways:
CODE

string str,strTotal;
ifstream in;
in.open("myfile.txt");
getline(in,str);
while ( in ) {
  strTotal += str;
  getline(in,str);
}

This will read the file line by line and append it to a string...keep in mind this method will take out the newlines, so if you want them, you'll have to put them in yourself...

There are other methods as well.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:09AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month