Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 08-07-2009
Member
 
Join Date: Apr 2008
Posts: 61
To find if end of the file in C++

Hello,

I am having this question with C++.

I want to know how to define a function to check if a file exists or not?
IF the file exist I want to know if its the end of file or not?

Please help me with the same.
Reply With Quote
  #2  
Old 08-07-2009
Member
 
Join Date: Apr 2008
Posts: 40
Re: To find if end of the file in C++

You need to know the working of End of File Function
Here is the code of the function:
C++ provides a special function, eof( ), that returns nonzero (meaning TRUE) when there are no more data to be read from an input file stream, and zero (meaning FALSE) otherwise.
Rules for using end-of-file (eof( )):
1. Always test for the end-of-file condition before processing data read from an input file stream.
a. use a priming input statement before starting the loop
b. repeat the input statement at the bottom of the loop body
2. Use a while loop for getting data from an input file stream. A for loop is desirable only when you know the exact number of data items in the file, which we do not know.

Code:
#include <iostream.h>
#include <fstream.h>
#include <assert.h>

int main(void)
{
     int data;           // file contains an undermined number of integer values
     ifstream fin;     // declare stream variable name

     fin.open("myfile",ios::in);    // open file
     assert (!fin.fail( ));     
     fin >> data;        // get first number from the file (priming the input statement)
                              // You must attempt to read info prior to an eof( ) test.
     while (!fin.eof( ))      //if not at end of file, continue reading numbers
     {
          cout<<data<<endl;    //print numbers to screen
          fin >> data;               //get next number from file
     }
     fin.close( );       //close file
     assert(!fin.fail( ));
     return 0;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "To find if end of the file in C++"
Thread Thread Starter Forum Replies Last Post
error Firefox can't find the file at jar:file:///C:/Program Files (x86)/Mozilla Firefox/omni.ja!/ ermahgerd Technology & Internet 3 10-09-2012 10:50 AM
Cannot find file error when opening a file in Windows WalshJ Vista Help 18 16-06-2012 06:36 AM
error "media player cannot find file. if you are trying to bur,play,sync an item in your library it might point to a file that has been moved,renamed,or deleted" blanny431 Media Player 5 03-03-2011 11:30 PM
Find nbi file on DVD Afia Windows Software 4 24-11-2009 10:22 AM
Find any file containing a string? Calab Vista Help 15 06-11-2008 03:56 PM


All times are GMT +5.5. The time now is 10:46 AM.