Thread: C++, files

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    12

    C++, files


    I have a question for yall,
    I would like to read from a file. Now I would like to read only one line. I would like to read that line string by string until that one line is done. I would then like to exit.
    Okay, now here is what is done.
    I can read from a file.
    I know how to read one string at a time, and I know how to read in an entire line using getline()
    I can exit

    I am not sure how to use the >> command to only read string by string for one line.

    I was trying something like

    string a;
    input2.open("out123456789.txt");
    while (a!=eol)
    input2>>a;

    Thanks for the help
    Hopefully I have made myself clear.

    Sincerely,
    Ronald
    This message is a natural product made from recycled electrons. The slight
    variations in spelling and grammar enhance its individual character and
    beauty and in no way are to be considered flaws or defects.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    Perhaps you could read one line at a time from the file (using getline) and assign it to a strstream. Then read from the strstream as if you're reading a string from a file. That will break it up into individual strings.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <strstream>
    using namespace std;
    
    int main(void) {
      string a;
      string line;
      ifstream input;
      strstream temp;
    
      // read a line from the file
      input.open("test.txt");
      getline(input, line);
      input.close();
    
      // Now break the line into individual strings
      temp << line;
      while (!temp.eof()) {
        temp >> a;
        cout << a << endl;
      }
      return 0;
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo