File help

This is a discussion on File help within the C++ Programming forums, part of the General Programming Boards category; I have program that goes through a file of numbers (each number is on a new line) it pulls the ...

  1. #1
    Unregistered
    Guest

    File help

    I have program that goes through a file of numbers (each number is on a new line) it pulls the number from the file, tests it against the number entered by the user. Right now it keeps looping through the file forever cause it can't find the number, how do I make it so that it will stop looking when it reaches the end of the file ?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    If you are using an ifstream, add the condition to your while loop to break if the stream encounters eof;

    Code:
    while(!mystream.eof){
    
    //Pull it data
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by Fordy
    If you are using an ifstream, add the condition to your while loop to break if the stream encounters eof;

    Code:
    while(!mystream.eof){
    
    //Pull it data
    }
    Just a minor change. I think parentheses are required on the call to eof().

    while(!mystream.eof()){

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Doh.......very true........my bad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. 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
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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