1 / 21

Streams and Files

Streams and Files. Chapter 12. Streams. Stream A transfer of information in the form of a sequence of bytes I/O Operations: Input: A stream that flows from an input device ( i.e.: keyboard, disk drive, network connection) to main memory

erich-bond
Download Presentation

Streams and Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Streams and Files Chapter 12

  2. Streams • Stream • A transfer of information in the form of a sequence of bytes • I/O Operations: • Input: A stream that flows from an input device ( i.e.: keyboard, disk drive, network connection) to main memory • Output: A stream that flows from main memory to an output device ( i.e.: screen, printer, disk drive, network connection)

  3. ios class • The class is designed to be a base class for all of the hierarchy of stream classes • Three most important features • Formatting flags • The error-status flags • File operation mode

  4. The base class ios and its derived classes ios ostream fstream istream ifstream frstream ofstream iostream

  5. Formatting flags • Set of enum definitions in ios • Specify choices for various aspects of input and output format and operations • cout.flags ( ios::right | ios::hex | ios::showbase );

  6. Manipulators • Manipulators are formatting instructions inserted directly into a stream e.g. endl • cout<<“Hello World”<<endl;

  7. Functions • ios class contains a number of functions for setting formatting flags and perform other tasks • cout.fill(‘*’); • cout.width(14);

  8. The istream and ostream class • Derived from ios class and perform input specific activities

  9. Cont’d • Ostream class handles output activities

  10. Stream Errors • What happen if user input nine instead 9 • The stream error-status flags are defined as enum in ios class that operate on input/output operations

  11. Example int main () { int i; while(true) { cout<<"\nEnter an integer: "; cin>>i; if(cin.good()) { cin.ignore(10,'\n'); break; } cin.clear(); cout<<"Incorrect input"; cin.ignore(10, '\n'); } cout<<"Interger is: "<<i; getch(); return 0; } To get rid of extra characters that were left with previous input we use ignore(MAX, DELIM)

  12. Disk file I/O with streams • Most programs needs to save data to disk and read it back in • Ifstream use for input and ostream for output • fstream is use for both input/output • ifstream, ofstream and fstreamclasses are declared in FSTREAM file • Formatted file: Data stored in file is not exact representation of data as they were in internal memory

  13. Cont’d • Unformatted file: Data stored in file is exactly same as they are stored in internal memory is binary file or unformatted file Program Buffer Transfer handled by a device driver Transfer handled by iostream library File Computer Memory Disk or Tape

  14. Example int main () { • ofstreammyfile; • myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; • myfile.close(); getch(); return 0; }

  15. Step by step understanding • myflie is an object of ofstream class • To open a file with a stream object we use its member function open() open (filename, mode); where mode is optional • When finished with input and output operations the file should be closed myfile.close();

  16. Optional file modes

  17. Reading data from file int main () { string line; ifstreammyfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; }

  18. Checking stat flags • bad() Returns true if a reading or writing operation fails. For example in the case that we try to write to a file that is not open for writing or if the device where we try to write has no space left. • fail() Returns true in the same cases as bad(), but also in the case that a format error happens, like when an alphabetical character is extracted when we are trying to read an integer number. • eof() Returns true if a file open for reading has reached the end. • good() It is the most generic state flag: it returns false in the same cases in which calling any of the previous functions would return true

  19. Get and put stream pointers • All i/o streams objects have, at least, one internal stream pointer • ifstream, has a pointer known as the get pointer that points to the element to be read in the next input operation. • ofstream, has a pointer known as the put pointer that points to the location where the next element has to be written.

  20. Cont’d • tellg() and tellp(): Return an integer value representing current position of get stream pointer(tellg) and put stream pointer (tellp) • seekg() and seekp(): To change the position of get and put stream pointers seekg ( offset, direction );seekp ( offset, direction );

  21. Example int main () { long begin,end; ifstreammyfile ("example.txt"); begin = myfile.tellg(); myfile.seekg (0, ios::end); end = myfile.tellg(); myfile.close(); cout << "size is: " << (end-begin) << " bytes.\n"; return 0; }

More Related