File input and output --c ++ Review (7)

First, the standard IO library

       1. Console write: <iostream>, comprising cin, cout objects

       2. write file: <fstream>

       3. The read and write the string: <sstream>

Second, the input file output *

       1. Open the file

       1.1 to create a file object constructor function to open a file: ofstream outfile ( "test.txt", ios :: out); The first parameter is the file path, the second parameter is the open mode

       1.2 open function to open a file:

1 ofstream ofile
2 ofile.open("/tmp/test.txt",ios::out)

       1.3 Open mode:

       app added; ATE file pointer at the end; binary binary file open; to enter in open mode only; open the output OUT, write-only; the trunc cover, remove the file data;

       You may incorporate "|" is operator specifies a plurality of modes: ios :: in || ios :: out ios :: in || ios :: binary

       1.4 Examples

1  // write file 
2  void Write ()
 . 3  {
 . 4      ofstream OUT ;
 . 5      OUT .Open ( " test.txt " , iOS :: OUT );
 . 6      IF (! OUT .fail ()) // determines whether the file is opened successfully 
. 7        {
 . 8          OUT << " Tim " ;
 . 9          OUT << " mail " ;
 10          OUT .flush (); // Flushes the buffer 
11        }
 12       OUT .close (); // Close the stream 
13  }
 14  
15  // read file 
16  void Read ()
 . 17  {
 18 is      the ifstream in ;
 . 19      in .Open ( " test.txt " , iOS :: in );
 20 is      char C;
 21 is      the while (! in .EOF ()) // determines whether the end of file 
22 is      {
 23 is        C = in . GET ();
 24        COUT << C;
 25      }
26     cout<<endl;
27     in.close();
28 }

 

Guess you like

Origin www.cnblogs.com/jiang-021/p/11531018.html