Transfer from: http://www.cnblogs.com/me115/archive/2010/09/20/1831555.htmlC++ file stream: FStream//File stream ifstream//input file stream Ofstream// Output file stream //Create a text file and write information//output information to the same screen as output information to the file #include#includevoid main () {Ofstream F1 ("D:\\me.txt"); Open file for write, if the file does not exist, create it if (!F1) return; Open file failed to end run F1<<SETW << "name:" << "East" <<endl; Use the Insert operator to write the contents of a file F1<<SETW << home address: << Zhengzhou, Henan <<endl; F1.close (); Close file} After running open file D:\me.txt, its contents are as follows: Name: William East Home Address: Henan Zhengzhou File operation: Open file name Note the slash in the pathname to double-write, such as: "D:\\MYFILES\\README.T XT "File Open with options: Ios::in = 0x01,//read, File not present (ifstream default open mode) Ios::out = 0x02,//write, file does not exist, and if the file already exists, empty the original content (OFST Ream default open mode) Ios::ate = 0x04,//When the file is opened, the pointer is at the end of the file. Can change the position of the pointer, and in and out combined with Ios::app = 0x08,//write, the file does not exist to create, if the file already exists in the original file content after the new content, the pointer position is always at the end of the Ios::trunc = 0x10,//Read the text before reading and writing Piece length truncation to 0 (default) ios::nocreate = 0x20,//file does not exist when error occurs, often and in or app federated use Ios::noreplace = 0x40,//File present error, common and out combined useIos::binary = 0x80//binary Format File Protection mode selection: Filebuf::openprot; The default compatible sharing mode Filebuf::sh_none; Exclusive, do not share filebuf::sh_read; Read Shared filebuf::sh_write; Write shared open file method call constructor when specifying file name and open mode Ifstream f ("D:\\12.txt", ios::nocreate); The default is to open the file in Ios::in mode, the operation fails when the file does not exist Ofstream F ("D:\\12.txt"); The file FStream f ("D:\\12.dat", Ios::in|ios::out|ios::binary) is opened by default in Ios::out mode; Open a binary file by reading and writing using the Open member function FStream f; F.open ("D:\\12.txt", ios::out); Use the Open function to check if successful opening succeeds when working with multiple files using the same object: if (f) {...} The FStream object is not available for Ifstream, Ofstream objects. if (F.good ()) {...} Failure: if (!f) {...} The operator has overloaded if (F.fail ()) {...} Read and write operations use the <<,>> operator only to read and write text files, which can cause errors in binary files. Functions that use a function member get, PUT, read, write, and so on are often used in conjunction with the Read Gcount () to obtain the actual number of bytes read. Read-write binary note the ios::binary must be specified in the open mode, otherwise the read-write error will be read and write with Read\write, and cannot be manipulated using the insert, extract operator, or an error will occur. Use the EOF () function to detect whether a file is read-end, use Gcount () to get the actual number of bytes to read close the file using the member function close, such as: F.close (); By using the Destructor object at the end of the lifetime, the file is checked for closure and the files that are not closed areClose the operation. Random Read and write files by moving the file read and write pointers, can be read and write in the file specified location. SEEKG (absolute position); Absolute movement,//input stream operation SEEKG (relative position, reference position); Relative Operation Tellg (); Returns the current pointer position SEEKP (absolute position); Absolute movement,//output stream operation SEEKP (relative position, reference position); Relative Operation TELLP (); Returns the current pointer position reference position: Ios::beg = 0//relative to the file header Ios::cur = 1//compared to the current position ios::end = 2///For the text file at the end of the file//to be able to Correctly read the data written to the file, the best between the data should be separated #includevoid main () {FStream f ("D:\\try.txt", ios::out); f<<1234<< ' <<3.14<< ' A ' << ' how is you '; Write Data f.close (); F.open ("D:\\try.txt", ios::in); int i; Double D; char c; Char s[20]; f>>i>>d>>c; Read Data f.getline (s,20); cout<<i<<endl; Show each data cout<<d<<endl; cout<<c<<endl; cout<<s<<endl; F.close ();} Run Result: 12343.14AHow is youpress any key to continue display the contents of the text file//use Get () to read one character at a time--------------------------------scheme one # Includevoid Main () {ifstream fin ("d:\\ introduction. txt", ios::nocreate); if (!fin) {cout<< "File Open error!\n "; Return } char C; while ((C=fin.get ())!=eof) cout<<c; Note the judgment of the end condition Fin.close ();} Use Get (char *,int n,char delim= ' \ n ') to read more than one character at a time----scheme two//skillfully take advantage of text files that do not have the character ' s ' feature to be read #includevoid main () {ifstream fin ("d:\ \ introduction. txt ", ios::nocreate); if (!fin) {cout<< "File open error!\n"; Return } Char c[80]; while (Fin.get (c,80, '!=null ') cout<<c; Note the judgment of the end condition Fin.close ();} Read file---------------------------scheme with read (char *,int N) three #includevoid main () {ifstream fin ("d:\\ introduction. txt", ios::nocreate ); if (!fin) {cout<< "File open error!\n"; Return } Char c[80]; while (!fin.eof ())//Determine if the file read-end {fin.read (c,80); Cout.write (C,fin.gcount ()); } fin.close ();} Copy file//binary file Operation example #includevoid main () {ifstream fin ("C:\\1.exe", ios::nocreate|ios::binary); if (!fin) {cout<< "File open error!\n"; Return } ofstream Fout ("C:\\2.exe", ios::binary); Char c[1024]; while (!fin.eof ()) {fin.read (c,1024); Fout.write (C,fin.gcount ()); } Fin.close (); Fout.close (); cout<< "Copy over!\n";}
C + + file read-write simple instance