issue with determining EOF

This is a discussion on issue with determining EOF within the C++ Programming forums, part of the General Programming Boards category; I have a file with a random number of integers, all on their own line, and the program needs to ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    416

    issue with determining EOF

    I have a file with a random number of integers, all on their own line, and the program needs to read them in. All is good, except being able to tell the computer where the end of the file is at. For a test file I put 21 numbers (positive and negative) in random order, and the program says there is 25 numbers, with a very small value for the highest and lowest number (ie -124325).

    Code:
    myfile>>inarray[n];
    high = inarray[n];
    low = inarray[n];
    
    while(inarray[n] != EOF){    //also tried while(inarray[n] != NULL)
    
        n++;
        myfile>>inarray[n];
        if(high < inarray[n])
            high = inarray[n];
        if(low > inarray[n])
            low = inarray[n];
    
    }
    
    cout<<"low: "<< low <<endl;
    cout<<"high: "<< high <<endl;
    cout<<"max: "<< n <<endl;

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    what is inarray?

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    The array i'm storying all the integers in to. The array is the input, hence calling it "inarray".

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The extraction operator never yields EOF. Use the boolean conversion of the stream object itself:
    Code:
    while(myfile) {
    Better yet, do it like this:
    Code:
    myfile>>inarray[n];
    high = inarray[n];
    low = inarray[n];
    ++n;
    
    while(myfile >> inarray[n]){    //also tried while(inarray[n] != NULL)
        if(high < inarray[n])
            high = inarray[n];
        if(low > inarray[n])
            low = inarray[n];
        ++n;
    }
    Of course, you'll have to adjust your code to account for the fact that n is one higher than in your version.

    What does this have to do with Linux, anyway? Moved.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    The programming is done on a linux based server, didnt really know if it should be in c++ or linux. Thanks for the idea, I will try it out.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    As a rule, it goes into Linux if it has Linux-specific concepts. When in doubt, don't put it in Linux.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 08:09 PM
  3. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 03:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM

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