#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    California
    Posts
    27
    Rep Power
    0

    Reading text in from a file in C++


    Hey all, I've got what I hope is a simple question. I'm trying to read in a line of text from a file one character at a time. After I read the line of text in from the file I try to output the line one character at a time. This works fine, except that it apparently isn't reading in any spaces in the line of text. Here's the part of my code you'd probably be interested in.


    char input;

    ifstream INFILE("test.txt", ios::in);
    if(!INFILE) {
    cerr<<"Test.txt could not be opened"<<endl;
    exit(1);
    }

    while (INFILE >> input)
    cout<<"letter "<<x++<<": "<<input<<endl;


    I really appreciate any help you guys could give me, I know it's kinda a newb. question. Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Malaysia
    Posts
    42
    Rep Power
    12

    use get


    Why dont you use INFILE.get() or INFILE.getline()

    the code will be:

    INFILE.get (input);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    California
    Posts
    27
    Rep Power
    0

    That worked


    Thanks, that worked great!

IMN logo majestic logo threadwatch logo seochat tools logo