C++ File and Stream Input/output


in this post i am describing c++ file and stream input/output. you can try it your self,

  • File I/O is based on streams. <fstream.h> must be
    included in your program
  • First operation is to open file
  ofstream outFile(char* filename,enum
open_mode mode);
  ifstream inFile(char* filename,enum open_mode
open_mode is
optional and defined in ios.h for appending, nocreate, noreplace
  • Test if open operation was successful
      // error occurred during open
  • Use << for output and >> for input
  • Output and input operators defined for standard types
  • Useful to provide << for your own classes
  friend ostream&
opeator<<(ostream& s,YourClass& c)
  • Use getline() or gets() members to read lines of text.
  inFile.getline(char* buffer,int size,char
  inFile.gets(char** addrOfPtrToChar,char
getline() you specify buffer and maximum size. With gets() you specify pointer
to a pointer. gets() will provide a buffer with each call and store pointer in
your pointer variable. Both functions will read newline from file but not place
it in string.
  • Test state of file operations using member functions
      // do something while reading data
      // give error message that file output
good() and fail() available. If stream is not good then all operations are
  • There is a position associated with the file. This is
    the byte location in the file where the next read or write operation will
    take place.
  • There are functions for manipulating the file position.
  // move to a specific byte position in file
  // move relative to a direction.  seek_dir = beg, cur, end
  // streamoff is offset from the seek_dir
  // clear eof or bad state
  // find file position
  streampos inFile.tellg();
  streampos outFile.tellg();
  • You can look ahead in an input file or “put
    back” unwanted characters in the input buffers.
  int inFile.peek();
  inFile.putback(char c);
  • There are two in-memory streams that are defined in
  char* p = new char(size);
  ostrstream outString(char *p, int size);
  istrstream inString(char *p,int size);
output is written to the string specified in the ostrstream create. Input is
read from the istrstream. All stream operations are valid on these streams
including stream position and state.
Output Formatting
  • Many formatting commands are set via member functions
  • Field width – minimum number of characters for next number or string
  •   cout.width(4);
  • Fill character – specify the character to fill field.
    Default is blank.
  •   cout.fill(`#’);
  • Many other format parameters can be set using setf()
    and unsetf() member functions.
    • Output base – ios::basefield {ios::oct, ios::dec,
    • Output justification – ios::adjustfield
    • Floating point format – ios::floatfield
      {ios::scientific, ios::fixed}
    • Show plus sign – ios::showpos

Join Our WhatsApp Broadcast @ +91 8129832162
Like Us on Facebook and Get Special Deals
Previous articleNOKIA LUMIA 900 Review
Next articleNOKIA N900 review
Sreenath Mukundan is a Passionate Blogger and Web Developer From Kerala,India. Currently, he is doing B-Tech Course on Computer Science Engg @ Jyothi Engineering College, Kerala. His area of interests are web development, SEO, Social Media Marketing and Finding network loopholes. Find Him On FACEBOOK