1 / 13

File I/O

File I/O. In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives (reads) or transmits (writes) a sequence of bytes.

Télécharger la présentation

File I/O

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. File I/O • In C++, I/O occurs in streams. • A stream is a sequence of bytes • Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives (reads) or transmits (writes) a sequence of bytes. • The reading and writing functions handle the streaming of bytes between the devices and memory.

  2. cin/cout vs. file I/O • The ostream and istream objects cout and cin are generated automatically (they are actually defined in the iostream header file) and associated with the standard output and input devices. • In order to read from or write to a file we must manually associate it with an input or output file stream object. • We must include the fstream header file for file I/O

  3. Sequential files • Sequential file = a file containing a collection of data. • C++ regards the file as a sequence of bytes. • We regard the file as a sequence of records in some specific order and write code to handle the file according to our view of its organization.

  4. Sequential files • In order to open a file for input or output, we must first create an ifstream or ofstream object and then associate the file with that object: ifstream inFile; // declare an ifstream object ofstream outFile; // declare an ofstream object inFile.open("data.txt", ios::in); // associate file data.txt (in the // current directory) with inFile // and open it for input. outFile.open("results.txt", ios::out); // associate file results.txt (in // the current directory) with // outFile and open it for output.

  5. Sequential files • More examples: • If results.txt already exists, ios::app appends to it, whereas ios::out clears it and then writes to it. fstream inOutFile; // declare an fstream object ofstream outFile; // declare an ofstream object inOutFile.open("data.txt", ios::in | ios::out); // associate file data.txt // with inOutFile and open // it for both input & output outFile.open("results.txt", ios::app); // associate file results.txt with // outFile and open it for output.

  6. File I/O • You may use the stream insertion and extraction operators to write to or read from a sequential file. • For user-defined types, the stream insertion and extraction operators must be overloaded accordingly. ofstream outFile; outFile.open("grades.txt", ios::out); char *name = "John Smith"; double grade = 98.8; outFile << name << " " << grade << endl;

  7. File I/O • Close the file stream when you don't need the file any longer (if you don't, it will be closed automatically via its destructor when it goes out of scope) ofstream outFile; outFile.open("grades.txt", ios::out); char *name = "John Smith"; double grade = 98.8; outFile << name << " " << grade << endl; outFile.close();

  8. operator>> and whitespace • operator>> ignores whitespace: • What if we want to read the whitespace as well? • Use the get() function fstream iofile("test.txt", ios::in|ios::out); iofile << 'a' << ' ' << 'b'; iofile.seekp(0); char x, y; iofile >> x >> y; // here, x is 'a' and y is 'b'. The empty space between them // has been skipped.

  9. get() • Use #1 : with no arguments • ch = inFile.get() ; // reads one character and assigns it to ch • Use #2 : with one argument • inFile.get(ch); // reads one character and assigns it to ch fstream iofile("test.txt", ios::in|ios::out); iofile << 'a' << ' ' << 'b'; iofile.seekp(0); char x, y; iofile >> x >> y; // here, x is 'a' and y is ' '

  10. put(), getline() • put() is similar to get() but is used for writing a single character • getline(char array, size, delimiter)reads size-1 characters or until the delimiter is found and places the result in the char array (the array will be null terminated). • It is used to read whole lines.

  11. peek(), seekg(), seekp() • inFile.peek() is used to look at the next character in the stream without actually removing it from the stream • inFile.seekg(n)moves the get pointer to the nth byte from the beginning of the file • outFile.seekp(n)moves the put pointer to the nth byte from the beginning of the file

  12. Unformatted I/O • To read or write raw bytes, use the read() and write() functions. • outFile.write(buffer, n) writes n bytes from buffer to outFile. • buffer must be the address of a string. If it is not, typecast it accordingly. • inFile.read(buffer, n) reads n bytes from inFile and places them in buffer. • buffer must be the address of a string. If it is not, typecast it accordingly.

  13. binary mode • In many cases we want to open a file in binary mode. • Possible reasons: • Text files are represented differently in different systems and we want to avoid related peculiarities. • Applications such as compression require it. • To process a file in binary mode we must open it with ios::binary • In that mode, << and >> do NOT work properly. Instead use read() and write().

More Related