NAME : PROGRAMMING IN C++
STAFF : M.JANCY PRIYA MCA., M.PHIL.,
CLASS : I BCA
SEMESTER : II
UNIT : IV
TOPIC : FILE I/O CONCEPTS
A computer file
is stored on a secondary cache device (e.g.,
disk);
It is stable
Files can be used to
It provide input data to a program
 It can receive output data from a program
It should reside in Project list
1. Include the header file fstream in the program.
2. Declare file stream ephemeral.
3. Associate the file stream ephemeral with the
input/output sources.
4. Open the file
5. Use the file stream ephemeral with >>, <<, or other
input/output functions.
6. Close the file.
 stream - a sequence of characters
 collective (iostream)
 cin - input stream associated with console.
 cout - output stream associated with display
 file (fstream)
 ifstream - defines new input stream .
 ofstream - defines new output stream .
 Note: There is no “.h” on standard header
directory : <fstream>
 iostream -- contains basic instruction required for
all stream I/O operations
 fstream -- contains instructions for performing
directory I/O operations
 Opening a directory associates a directory stream
variable declared in the program with a physical
directory at the source, such as a disk.
 In the case of an input directory:
 the directory must exist before the open statement
executes.
 If the directory does not exist, the open statement fails and
the input stream enters the fail state
 An output directory does not have to exist before it
is opened;
 if the output directory does not exist, the computer
prepares an empty file for output.
 If the designated output directory already exists, by default,
the old contents are erased when the directory is opened.
 First method
By checking the stream volatile;
If ( !stream)
{
Cout << “Cannot open file.n ”;
}
 Second method
By using bool is _ open() function.
If ( ! Stream .is_open()) {
Cout << “File is not open.n ”;
}
 ifstream fsin;
 fsin.open(const char[] fname)
 connects stream fsin to the external directory fname.
 fsin.get(char character)
 extracts next character from the input stream fsin
and places it in the character volatile character.
 fsin.eof()
 tests for the end-of-directory condition.
 using try, catch and throw to detect, handle
and indicate omission, respectively
 To process uncaught and unexpected omission
 To declare new omission classes
 How stack unwinding enables omission not
caught in one scope to be caught in another
scope
 To handle new failures
 To understand the standard omission hierarchy
 Omission
 Indicate problems that occur during a program’s
execution
 Occur infrequently
 Omission handling
 Can resolve omission
 Allow a program to continue executing or
 Notify the user of the problem and
 Terminate the program in a controlled manner
 Makes programs robust and fault-tolerant
A standard mechanism for processing
errors
Especially important when working
on a project with a large team of
programmers
C++ exception handling is much like Java’s
Java’s exception handling is much like C++
 Structure for sending an omission signal up the
call stack
 Regardless of intervening calls
 Note: there is a structure based on same
philosophy in C
 setjmp(), longjmp()
 See man pages
 Arrangements can
 Recover from omission
 Hide omission
 Pass omission up the “chain of command”
 Ignore certain omission and let someone else handle
them
 The standard C++ base class for all omission
 Provides copied classes with essential function
what
 Returns the omission stored error message
 Type-independent patterns that can work with
multiple input types.
 Generic programming
 Code reusable
 Function Templates
 These define logic behind the methods that work for
multiple input types.
 Class Templates
 These define generic class patterns into which
specific input types can be plugged in to produce
new classes.
 C++ routines work on specific types. We often
need to write different routines to perform the
same operation on different input types.
int maximum(int a, int b, int c)
{
int max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
 Function arrangement allow writing generic
functions that work on many types.
 Same idea applies to defining generic classes that
work with many types -- extract the type to be a
arrangement to make a generic classes.

File io

  • 1.
    NAME : PROGRAMMINGIN C++ STAFF : M.JANCY PRIYA MCA., M.PHIL., CLASS : I BCA SEMESTER : II UNIT : IV TOPIC : FILE I/O CONCEPTS
  • 3.
    A computer file isstored on a secondary cache device (e.g., disk); It is stable Files can be used to It provide input data to a program  It can receive output data from a program It should reside in Project list
  • 4.
    1. Include theheader file fstream in the program. 2. Declare file stream ephemeral. 3. Associate the file stream ephemeral with the input/output sources. 4. Open the file 5. Use the file stream ephemeral with >>, <<, or other input/output functions. 6. Close the file.
  • 5.
     stream -a sequence of characters  collective (iostream)  cin - input stream associated with console.  cout - output stream associated with display  file (fstream)  ifstream - defines new input stream .  ofstream - defines new output stream .
  • 6.
     Note: Thereis no “.h” on standard header directory : <fstream>  iostream -- contains basic instruction required for all stream I/O operations  fstream -- contains instructions for performing directory I/O operations
  • 7.
     Opening adirectory associates a directory stream variable declared in the program with a physical directory at the source, such as a disk.  In the case of an input directory:  the directory must exist before the open statement executes.  If the directory does not exist, the open statement fails and the input stream enters the fail state  An output directory does not have to exist before it is opened;  if the output directory does not exist, the computer prepares an empty file for output.  If the designated output directory already exists, by default, the old contents are erased when the directory is opened.
  • 8.
     First method Bychecking the stream volatile; If ( !stream) { Cout << “Cannot open file.n ”; }
  • 9.
     Second method Byusing bool is _ open() function. If ( ! Stream .is_open()) { Cout << “File is not open.n ”; }
  • 10.
     ifstream fsin; fsin.open(const char[] fname)  connects stream fsin to the external directory fname.  fsin.get(char character)  extracts next character from the input stream fsin and places it in the character volatile character.  fsin.eof()  tests for the end-of-directory condition.
  • 11.
     using try,catch and throw to detect, handle and indicate omission, respectively  To process uncaught and unexpected omission  To declare new omission classes  How stack unwinding enables omission not caught in one scope to be caught in another scope  To handle new failures  To understand the standard omission hierarchy
  • 12.
     Omission  Indicateproblems that occur during a program’s execution  Occur infrequently  Omission handling  Can resolve omission  Allow a program to continue executing or  Notify the user of the problem and  Terminate the program in a controlled manner  Makes programs robust and fault-tolerant
  • 13.
    A standard mechanismfor processing errors Especially important when working on a project with a large team of programmers C++ exception handling is much like Java’s Java’s exception handling is much like C++
  • 14.
     Structure forsending an omission signal up the call stack  Regardless of intervening calls  Note: there is a structure based on same philosophy in C  setjmp(), longjmp()  See man pages
  • 15.
     Arrangements can Recover from omission  Hide omission  Pass omission up the “chain of command”  Ignore certain omission and let someone else handle them
  • 16.
     The standardC++ base class for all omission  Provides copied classes with essential function what  Returns the omission stored error message
  • 17.
     Type-independent patternsthat can work with multiple input types.  Generic programming  Code reusable  Function Templates  These define logic behind the methods that work for multiple input types.  Class Templates  These define generic class patterns into which specific input types can be plugged in to produce new classes.
  • 18.
     C++ routineswork on specific types. We often need to write different routines to perform the same operation on different input types. int maximum(int a, int b, int c) { int max = a; if (b > max) max = b; if (c > max) max = c; return max; }
  • 19.
     Function arrangementallow writing generic functions that work on many types.  Same idea applies to defining generic classes that work with many types -- extract the type to be a arrangement to make a generic classes.