Reading in input from file with multiple values per line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • unknown418
    New Member
    • Oct 2007
    • 5

    Reading in input from file with multiple values per line

    I am confused what is the efficient way to read in a file with multiple values per line. Each value is separated by a space character. The values could be characters or real values.

    I am using ifstream to get the file by passing it a c string as the filename but I am using a c++ string to get each line with getline(). But to get the individual values I think the only way is to use strtok() which needs a c string as input. To convert a string to a double would I use atod(). This seems very difficult.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Use stringstreams. Please google and search the forums first, and then repost if you don't understand the material.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by unknown418
      But to get the individual values I think the only way is to use strtok() which needs a c string as input. To convert a string to a double would I use atod(). This seems very difficult.
      I should think so since that is C.

      You say the values are separated by a space?

      Then you can read them using the ifstream >> operator. Of course, you need to know the type of the value for this to work.

      Comment

      • unknown418
        New Member
        • Oct 2007
        • 5

        #4
        I dumped my idea and started to use the string streams. Only problem is that when I read in from the obj file I get an extra character before the end of the line. I don't get this problem if the file is an txt file.

        For example:
        Code:
        ifstream iF(fileName.c_str());
        string line;
        getLine(iF, line);
        istringstream sT(line);
        char ch;
        
        while(!sT.eof()){
           sT >> ch;
           cout << ch << endl;
        }
        If the fileName is a obj file, and the first line is f 1 2 3. The output is:
        f
        1
        2
        3
        3

        If it is a txt file get:
        f
        1
        2
        3

        Why is that?

        Comment

        • Studlyami
          Recognized Expert Contributor
          • Sep 2007
          • 464

          #5
          Code:
          while(!sT.eof()){
             sT >> ch;
             cout << ch << endl;
          }
          Think about that while loop and step through it like you were a computer

          On the first loop ch becomes 1. Output 1.
          Check to see if you reached EOF.--->you havn't
          ch becomes 2. Output 2.
          Check to see if you reached EOF. ---> you havn't
          ch becomes 3. -->you just recieved 3 from the stream so you havn't reached the end yet
          Output 3.
          Check to see if you reach EOF. --->you havn't the last number was 3.
          sT>> now hits the end of file. "ch" is remaining the same as the last value.
          Output "ch" which is 3.

          you can fix this by copying the character before the check.
          Code:
          sT>>ch;
          while(!sT.eof()){
             cout << ch << endl;
             sT >> ch;

          Comment

          • unknown418
            New Member
            • Oct 2007
            • 5

            #6
            Studlyami thanks for the help. I didn't realize that I have to read trailing white spaces before sT.eof() would return true. I thought that after reading in the last character, sT.eof() would return true and that getline would have removed those trailing whitespaces. Is there a quick function to remove trailling whitespaces from a string.

            Comment

            Working...