Re: ifstream code to read a paragraph

From: Josh Sebastian (curien_at_cox.net)
Date: 10/31/03


Date: Thu, 30 Oct 2003 20:38:59 -0500

On Thu, 30 Oct 2003 22:52:52 +0000, CJ wrote:

> snip of code below - trying to read in a file with a paragraph in it.
> The file is being read in but only the first word of the paragraph displays
> The CHAR probably has to be changed but to what can't find a reference in my
> book
> I'm just practicing on how to get my program to read from a text file
> Any help appreciated - I'm just learning
>
> int main()
> {
> ifstream inFile;
> char message [81];
>
> inFile.open("message.txt");
> inFile >> message;
>
> cout << message <<endl;

That's because you only asked it to read one word. Remember that
operator>> will stop reading when it sees whitespace. Also note that the
first word could be more than 80 characters, causing your program to have
a buffer overrun. Much safer is to use the std::string class.

How do you define a paragraph? Is it defined by two newlines in a row
(roughly how I am splitting my paragraphs in this post)? Does it end (and
the next one start) when you see a newline followed by a tab? Or maybe
something else? Think about these things (design decisions), try again,
and if you're still having trouble, ask for more help.

Josh