Results 1 to 5 of 5

Thread: eof() struggels to handle my file, why?

  1. #1
    Join Date
    Mar 2008
    Location
    Nottingham
    Beans
    167
    Distro
    Ubuntu

    [solved] eof() struggels to handle my file, why?

    Hi,
    I have the following problem. I am reading a very large file into my program (around 15000000 lines). But my !eof() test does not seem to recognise the end of file, which results in my program crashing.
    Below the essential bit of the code:

    PHP Code:
    //variables
    ifstream inEdges ("homogEdges.dat");
    vector<unsigned long longVertex;

       if(
    inEdges.good())
        {
            
    inEdges>>invalue;
           
            while(!
    inEdges.eof())
            {
               
    //cout<<invalue<<endl;
                
    Vertex.push_back(invalue);
                
    inEdges>>invalue;
                
    i++;
                if(
    i>30000000)
                {
                    
    cout<<invalue<<endl;
                    
    cout<<"this should not happen as the file is smaller"<<endl;
                }
               
            }
            
    inEdges.close();
        } 
    my cout test is reached every single time, even tho i should not be any bigger than 25000000.
    The file i am reading in looks like this:
    233245 2345325
    2341 21345
    32466 234567
    etc. (for ~12000000 lines)
    obviously eof() cannot handle the file size? Is there any other way of testing for the end of file, as I don't want to do a for loop manually entering the amount of lines in the file every single time.

    Thanks
    Last edited by abraxas334; December 10th, 2008 at 01:36 PM. Reason: found problem myself, sorry for bothering u with this
    alea iacta est

  2. #2
    Join Date
    May 2008
    Location
    UK
    Beans
    1,450
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: eof() struggels to handle my file, why?

    Does it work with a smaller file - say 10 lines ?

    If you say your code is going beyond the end of the file - what is the code reading after the end of the file ?
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  3. #3
    Join Date
    Mar 2008
    Location
    Nottingham
    Beans
    167
    Distro
    Ubuntu

    Re: eof() struggels to handle my file, why?

    yes i tried smaller files they worked
    I have found the problem now, sorry i had a stray comma in my file which was not read by the program!

    should have noticed it earlier but obviously difficult with such a big file!

    Thanks tho
    alea iacta est

  4. #4
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: eof() struggels to handle my file, why?

    Also do not forget to read the fail() value of your input stream. This will tell you if the program encountered a non-number in the input file.

  5. #5
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,274
    Distro
    Kubuntu

    Re: eof() struggels to handle my file, why?

    I would suggest that the code be revisited and simplified:
    PHP Code:
      ifstream inEdges("homogEdges.dat");
      
    vector<unsigned long longVertex;

      if (
    inEdges)
      {
        
    unsigned long long invalue;

        while (
    inEdges >> invalue)
        {
          
    Vertex.push_back(invalue);
        }
      } 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •