Thread: how to check end of file ?

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    92

    Question how to check end of file ?

    Code:
    ifstream fin( "file.dat") ;
    
    while(fin) // end of file checking
    {
    // do something
    }
    question 1 :
    fin is a object. it is not a boolean variable . so how can i use this to check end of file ?




    question2:
    what are other ways i can check end of file ?

    thanks
    blue_gene

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >fin is a object. it is not a boolean variable
    It does, however, have an implicit conversion operator that allows it to be used in a boolean context.

    >so how can i use this to check end of file ?
    You don't, that's not a sound way to do it.

    >what are other ways i can check end of file ?
    One way (assuming string input):
    Code:
    while ( getline ( fin, str ) ) {
      ...
    }
    Another way:
    Code:
    for ( ; ; ) {
      // Get input
      if ( fin.eof() )
        break;
      ...
    }
    Naturally the first is preferable if you can manage it. I can't be more specific since you didn't say what you are using for input.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-26-2009, 12:24 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  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