Re: ifstream code to read a paragraph

From: Chris \( Val \) (chrisval_at_bigpond.com.au)
Date: 10/31/03


Date: Sat, 1 Nov 2003 02:19:29 +1100


"CJ" <delgato@adelphia.net> wrote in message
news:m9tob.373$Bf7.283442@news1.news.adelphia.net...

[snip, and please do not top post, re-formatted below]

Note also, please leave some context, as to who you are
replying to.

| "CJ" <delgato@adelphia.net> wrote in message
| news:8Dgob.114$Bf7.82036@news1.news.adelphia.net...
| > 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;
| >
| >
|
| do you mean to take out the word char and replace it with the word string??
|

I'll assume you are referring to Josh' advice.

Yes, but you need to do more that just replace the
variable type. Following is a basic skeleton to get
you started.

Just about any book should have many examples for the
use of std::string and std::getline(), as they go hand
in hand in just about every character mode program you
can think of.

With the above in mind, you should not require more than
a few lines of code to complete the exercise.

# include <iostream>
# include <fstream>
# include <string>
  using namespace std;

int main()
 {
  string Buffer;
  ifstream InFile( "MyFile.txt" );

  if( !InFile ) // Check if file could be opened.
   {
    cout << "Could not open file for input " << endl;
    return EXIT_FAILURE;
   }

  // Your code goes here...
  // Look up the std::getline() function to read a line into Buffer.

  return 0;
 }

Have a go at it, and post back with your attempt, if
your stuck.

Cheers.
Chris Val



Relevant Pages