File Empty?

This is a discussion on File Empty? within the C++ Programming forums, part of the General Programming Boards category; I am trying to read from a file with a list of numbers and add them to a vector. Initially, ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    45

    File Empty?

    I am trying to read from a file with a list of numbers and add them to a vector. Initially, this file will be empty, but after the user has entered more data(from other parts of my code) it will contain the list of numbers.

    My problem is that when i ouput the vector to the screen, tlist[0] is rubbish, or if i initialize data it will be what i initialized it to. If i read in an integer before the while loop, it takes care of the problem...only the first time however. Once there is data in the file, it gets screwed up.

    Any thoughts? Am I going about this the right way?

    Code:
    ifstream filein("List.txt");
    
    vector <int> tlist;
    int counter = 0, data;
    
    while( ! filein.eof() ) 
    {
        filein >> data;
        tlist.push_back(data);
    }
    filein.close();
    Output when the file starts empty:
    -89545645
    23
    45
    34

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You shouldn't control your loop with eof(). You should use the return value of operator>> instead:
    Code:
    while (filein >> data)
      tlist.push_back(data);

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    Thanks, that solved it.

    Should i stay away from eof() in general?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Using the return value of the read operation (whether it is operator>>, getline, or something else) is probably better since it accounts for other errors as well. The point is that you want to check to see if the read worked before using the value that was read in. In your original code, you don't check to make sure the read worked before you push back value on to your vector.

  5. #5

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Quote Originally Posted by quizkiwi
    Output when the file starts empty:
    -89545645
    23
    45
    34
    What output? You code sample you provided does not have any output only input.

    Another way to read in data from a file and into a vector would be to use the copy function along with a back_inserter:

    Code:
    #include <vector>
    #include <fstream>
    #include <algorithm>  // For the copy function
    #include <iterator>   // Might be needed for back_inserter
    #include <iostream>
    using namespace std;
    
    int main()
    {
        ifstream filein("List.txt");
        vector<int> tlist;
    
        // Get all ints from file and insert into vector tlist
        copy( istream_iterator<int>(filein), istream_iterator<int>(), back_inserter(tlist) );
    
        // Do stuff with vector tlist
    
        ...
    
        return 0;
    }
    [edit]That way of reading the file works if the file has data or is empty.[/edit]
    Last edited by hk_mp5kpdw; 07-13-2005 at 06:57 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM

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