210 likes | 393 Views
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
E N D
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 • Output: A stream that flows from main memory to an output device ( i.e.: screen, printer, disk drive, network connection)
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
The base class ios and its derived classes ios ostream fstream istream ifstream frstream ofstream iostream
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 );
Manipulators • Manipulators are formatting instructions inserted directly into a stream e.g. endl • cout<<“Hello World”<<endl;
Functions • ios class contains a number of functions for setting formatting flags and perform other tasks • cout.fill(‘*’); • cout.width(14);
The istream and ostream class • Derived from ios class and perform input specific activities
Cont’d • Ostream class handles output activities
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
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)
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
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
Example int main () { • ofstreammyfile; • myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; • myfile.close(); getch(); return 0; }
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();
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; }
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
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.
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 );
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; }