File content -> string?

This is a discussion on File content -> string? within the C++ Programming forums, part of the General Programming Boards category; Is there a way to get the contents of a file and place it into a string?
So far I ...

I've tried setting word = file, doing a while(file>>word), and some other things (such as setting word = file.open(), file.count, and things that generally didn't work).
Is there a way to set the contents from that file into the string for manipulation?

getline() reads in a line up to a newline, not just any white space, so vs[0] would be "#include <cstdlib>" instead of just "#include". I suggest using the >> operator rather than getline(). Alternatively, you could use a function search through the strings for "#include".

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

If you have code like
getline(cin, name);
and I enter "John Q. Public" and hit enter
then name == "John Q. Public".
But if instead you have code link
cin >> name;
and I enter "John Q. Public" and hit enter,
only "John" will be stored in name.

getline() keeps reading in until a newline (or EOF) is reached.
cin >> name; only reads up to whitespace.

If you'd prefer to store one word at a time without whitespace, use cin >>. If you want to store an entire line with spaces and tabs and such in it, then use getline(). I think for your purposes it'd be better to do the former.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.