Saturday, May 9, 2009

C++ file streams?

I'm kind of stumped on this one. I want to read in a .txt file with C++'s ifstream object and save it to a string object.





The problem is: I'm having trouble saving the entire contents of the file to a single string, so I can manipulate it.





How can I do that ? %26gt;.%26lt;

C++ file streams?
I would use an array of strings. First read the whole file and enumerate the number of lines involved lets call this n, use a while loop for doing this:


while(file.readline())


{


n++;


};


Create string array:


String[] arr = new String[n];


fill in the lines for each line in the file:


int m = 0;


while(file.readline())


{


arr[m] = file.readline().toString();


m++;


}





Then you can manipulate the array of strings, and when you want you can append the strings using a simple for loop.


Also consider the StringBuilder Class it might be a more precise solution.
Reply:dont use any feedbacks like new line tabs etc

peony

No comments:

Post a Comment