Thread: C++, files

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

    Join Date
    Sep 2003
    Rep Power

    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;"out123456789.txt");
    while (a!=eol)

    Thanks for the help
    Hopefully I have made myself clear.

    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
    Woodland Hills, Los Angeles County, California, USA
    Rep Power
    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.
    #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"test.txt");
      getline(input, line);
      // 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