C ++ (forty-seven) - file input and output streams

 1, file input and output streams

  Because the file is not as device monitor screen and keyboard as the default standard equipment, not like cout as global objects defined in advance , so we have to define your own object class.

  1. ifstream class, which is derived from istream class, to support input from a disk file.
  2. ofstream class, which is derived from ostream class to support output to a disk file.
  3. fstream class, which is derived from the iostream classes to support input and output to disk files.

Files Principle

    Open file has a file pointer , the initial position of the pointer specified by the I / O mode, each read and write all of the current position the file pointer. Each reads a byte, a byte pointer backward. When the file pointer to the last , will encounter the end of file the EOF (end of file is also one byte, which is a value of -1), then the stream object member function eof the value is not zero (usually set to 1 ), it indicates that the file is over .

    Close file, the disk file is actually associated with the stream file is released, the original work is also provided invalid, so you can not input or output to the file by file stream

    File Type: 1, ASCII files : each byte ASCII code format files are stored data, namely a byte to store one character, this file is an ASCII file (also known as character file).

2. Open the file:

Embodiment 1 : a file output stream, stream object file, the file is opened by the constructor of the class ofstream

    Format: ofstream (disk file name,  input and output );      

    If ofstream is 0 (false), it means open operation fails

    如:   ofstream fout1(fname,ios::out);

    Input and output may be used in combination can be used to "or" operator ( "|") manner, such as: fstream fout (fname, ios :: out | ios :: in)

Embodiment 2 : the input and output stream object file, stream object file, the file can be opened by the open function

    Format: file stream object .open (disk file name,  input and output );      

    Returns: 0 (false), open operation failed

    如:   fout.open(fname,ios::out)       

Close the file :

 After the reading and writing of the disk file open operation is completed, the file must be closed such as: outfile.close ();

 Mode 1:

#include <the iostream>
 the using  namespace STD; 
#include <the fstream> void main () 
{ char * fname = " D: /VS_programs/day_04/a.txt " ;
     // ofstream FOUT (fname, iOS :: OUT); / / not write back the default value 
    ofstream FOUT (fname); // build object file associated with an output stream and 
    FOUT << " Hello .... 111 " << endl; 
    FOUT << " Hello .... 222 " < < endl; 
    fout.close (); // read file
     // ifstream FIN (fname,ios::in);


    

    // do not write back the default value of 
    the ifstream FIN (fname); // create a stream of input objects and files associated 
    char CH;
     the while (. FIN GET (CH)) 
    { 
        COUT << CH; 
    } 
    fin.close (); 
    System ( " PAUSE " ); 
}

 

Option 2:

ofstream OUT ;              // output stream pointer OUT 
the ifstream in ;           // input stream pointer in 
OUT .Open ( " file.txt " );      // in a manner to open the file output file.txt 
 
wrtie_file ( OUT );      // write_file user to write a function for operating the out pointer to file 
out .close ();               // after completing operation must be shut down stream pointer. Otherwise it can not open the file pointer with other streams, such as the input stream pointer
               // do not call close () function, you can not make the other stream pointer or other processes from accessing the file 
 
in .open ( " file.txt " );         // to enter a way to open the file file.txt 
read_file (in );             // the read_file user is to write a function for operating in a pointer to a file 
in .close ();             // Close the input stream pointer 
 
/ * ----------- say one kind way ------------- * / 
as: 
ofstream out ( " file.txt " ); also means open file.txt, returned stream pointer out, but he is an implicit call open function, because the constructor ofstream class will have to call the open function 
the same way: 
ifstream in ( " file.txt " ); it is equivalent to the ifstream in ;   in .open ( " file.txt " );

 

 

ofstream out;
out.open ( "file.txt", ios :: App) / / additional content is to open the file

 

 3, to determine whether to open a successful and whether the end of the file


( 1 ) if the open succeeded 

ofstream OUT ( " file.txt " );
 IF (! OUT .is_open) 
{ 
    COUT << "fail open" << endl; 
} 
 
// Similarly ifstream in this way is determined. 

( 2 ) to the end of the file if 
the ifstream in ( " file.txt " );
 the while (! In .EOF) 
{ 
    fun ();    // if the file does not reach the end, on the implementation of fun () function 
}

 

4, access to a text file size

#include<iostream>
#include<fstream>
using namespace std;
 
void get_size(ifstream &in)
{
    long b,e;
    b=in.tellg();
    in.seekg(0,ios::end);
    e=tellg();
    long size=e-b;
    cout<<"size="<<size<<endl;
}
 
int main()
{
    ifstream in("file.txt");
    get_size(in);
    in.close();
    return 0;
}

 

5, to obtain the pointer and set stream

ifstream get a pointer pointing to the next element to be read
ofstream has a put pointer points to the next location to be written in the
case of tellg () and tellp () are used to return the get pointer, put pointer location, and does not need to input parameters
of seekg () and seekp () are used to set the location of the get pointer and put pointers, one of the two parameters, one of the first displacement amount, to the second position.

seekg ( ; 0, ios :: BEG) where // Set the get pointer location, from the beginning of the file position 0 positions


Reference blog: https: //blog.csdn.net/u014453898/article/details/54565974

 

Guess you like

Origin www.cnblogs.com/eilearn/p/10990073.html