Click to See Complete Forum and Search --> : ifstream in c++ ... more than one line in a single string


wxjunkie
09-29-2002, 02:41 AM
How can I assign more than one line from a simple text file or more than one word on a single line in a text file to one declared string?

Example... file.dat reads:
Sharky Forums
29 September 2002


I want to assign "Sharky Forums" from that file to a string named refName

ifstream inData;

string refName;

inData.open("file.dat")

inData >> refName;


How can I cause both words on the first line to be assigned to refName instead of just the first word, "Sharky" ??

gonphsn
10-02-2002, 04:20 PM
use the getline() function included in the string file.

getline(infile (or whatever you ifstream is declared as), refName (or whatever youve declared your variable to be), '\n'(or whatever you want your halt character to be))