Thread: File help

  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,793
    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,793
    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, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM