Hey guys, I'm stuck on an assignment for school, was wondering if you could help me out. So I have a text file similar to this:

Joe
Jim
Hello Jim,
Just wanted to say hello!

-Jim
EOF

This is supposed to be like a message system. The first line is the name of the sender, second line is name of the recipient, 3rd line is the subject, and all lines after that until it says EOF are part of the message body. So I know I can use getline to get the first 3 into separate variables, but the message body is in multiple lines and needs to be in one variable. I was wondering how I can do that, since getline can't get multiple lines...

Thanks!

Does your variable need to mark where new lines are? If not, you can just append each line to the same variable. If you do, just ad a null character(or a character of your choosing) after each line prior to appending.

fin.getline( message, MESSAGE_SIZE, '\0' );
-or-
getline( fin, message, '\0' );

These cause the input to read till a NULL is encountered, which should never happen. Thus, it will read till end of file, storing all to the single string variable. The newlines in the input file will be preserved in the string.

It's kind of a kludgy way to do it, but simple. There have been some other threads here on the topic, with good, but somewhat more complex solutions. Here's one.

Thanks, got it to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.