Grabbing input text file using a stream

This is a discussion on Grabbing input text file using a stream within the C++ Programming forums, part of the General Programming Boards category; I wrote some code to grab input from a text file. I know specifically the format of the text file ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    Grabbing input text file using a stream

    I wrote some code to grab input from a text file. I know specifically the format of the text file and the program was made tailored to only that specific format. I know this is pretty bad practice and if anyone can point me in the right direction, that would be great. But first I'd like to address a bug I'm getting.

    My program basically takes in a .txt file and echoes back the file in exactly the same format. The bug is that I get a last line of zeroes. Can anyone explain to me why?

    Code:
    //Copies everything in an input file to cout
    //usage: executable file1
    
    #include <iostream>
    #include <fstream>
    #include <sstream>
    
    using namespace std;
    
    int main(int argc, char** argv)
    {
      ifstream fin(argv[1]);
      string line;
    
      if (fin)
        {
          int nodes = 0;
          
          fin >> nodes;
    
          cout << nodes << endl;
        }
    
      //Grabs edge1, edge2, and cost
      while (fin)
        {
          //I need these to be int. I'm going to be doing something later with it.
          int x = 0, y = 0, z = 0;
          fin >> x >> y >> z;
          
          cout << x << " " << y << " " << z << endl;
        }
    }
    Here is the input for the .txt file.
    Code:
    50
    0 1 14
    1 2 13
    3 9 11
    9 11 5
    1 2 3
    Here is the output of the program.
    Code:
    50
    0 1 14
    1 2 13
    3 9 11
    9 11 5
    1 2 3
    0 0 0       //last line of zeroes

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Ah, this one is simple to explain.
    After having read the last line, the stream is still in a good state, because all reads succeeded.
    On the next iteration, you initialize all your variables to 0, then try to read (which fails). Thus, your variables are not modified and you print out 0s.

    To fix it, you may want to change the logic to perform the loop only if the read succeeded.

    Code:
      //I need these to be int. I'm going to be doing something later with it.
      int x = 0, y = 0, z = 0;
      while (fin >> x >> y >> z)
      {
          cout << x << " " << y << " " << z << endl;
      }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Input Stream constructor
    By yes2 in forum C++ Programming
    Replies: 0
    Last Post: 12-15-2010, 09:59 PM
  2. Using input stream to get string from file.
    By CPPN0OB in forum C++ Programming
    Replies: 7
    Last Post: 03-27-2010, 03:48 PM
  3. Getting an input stream to stop creating a file
    By Stevek in forum C++ Programming
    Replies: 3
    Last Post: 03-21-2003, 03:45 PM
  4. Input file stream problems
    By Shadow12345 in forum C++ Programming
    Replies: 10
    Last Post: 10-06-2002, 06:33 PM
  5. stripping punctuation from input file stream
    By asdfasdfasdf in forum C++ Programming
    Replies: 4
    Last Post: 10-01-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21