requirements:
Learn how to read and write text files
Learn how to read and write binary files
C + + file stream:
FStream //file stream
Ifstream //input file stream
Ofstream //output file stream
-
//create a text file and write information Output information to a file as output to the screen #include <iomanip.h> #include <fstream.h>void Main () {Ofstream F1 ( "d:\\me.txt"); //open file for writing, if the file does not exist create it if (!F1) return; //Open file failed then end run F1<<SETW () << "name:" << "cheap Orient" < <endl; //use the Insert operator to write the contents of the file F1<<setw (a) << "Home Address:" << "Zhengzhou, Henan" <<endl; F1.close (); //close file}
After running, open the file D:\me.txt, which reads as follows:
Name: William Oriental
Home address: Zhengzhou, Henan
File operation:
Open File
Filename
Note that the slash in the path name is double-written, such as:
"D:\\myfiles\\readme.txt"
File Open With options:
Ios::in = 0x01,Read, file does not exist (ifstream default open mode)
Ios::out = 0x02,Write, the file does not exist, and if the file already exists, empty the original content (ofstream default open mode)
Ios::ate = 0x04,When the file opens, the pointer is at the end of the file. Can change the position of the pointer, often combined with in and out
Ios::app = 0x08,Write, the file does not exist to create, if the file already exists in the original file content after writing new content, the pointer position is always the last
Ios::trunc = 0x10,Truncate the length of the file to 0 (default) before reading and writing
Ios::nocreate = 0x20,An error occurs when the file does not exist, often in conjunction with in or app
Ios::noreplace = 0x40,An error occurs when a file is present and used in conjunction with out
Ios::binary = 0x80binary format Files
File Protection options:
Filebuf::openprot;The default compatible sharing method
Filebuf::sh_none;Exclusive, not shared
Filebuf::sh_read;Read share
Filebuf::sh_write;Write sharing
Ways to open a file
Specify the file name and open mode when calling the constructor
Ifstream f ("D:\\12.txt", ios::nocreate);The default is to open the file as Ios::in, and the operation fails when the file does not exist
Ofstream f ("D:\\12.txt");Open files by default in Ios::out mode
FStream f ("D:\\12.dat", ios::in|ios::out|ios::binary);Open a binary file as read-write
Using the Open member function
FStream F;
F.open ("D:\\12.txt", ios::out);Use the Open function when working with multiple files using the same object
Check if Open successfully
Success:
if (f) {...}The FStream object is not available for Ifstream, Ofstream objects.
if (F.good ()) {...}
Failed:
if (!f) {...}! Operator is overloaded
if (F.fail ()) {...}
Read and write operations
Using the <<,>> operator
Only text files can be read and written, and used in binary files may produce errors.
Use function members get, put, read, write, and so on
The function that is often used in conjunction with Read is Gcount (), which is used to obtain the actual number of bytes read.
Read and write binary file considerations
Ios::binary must be specified in open mode, otherwise read-write error
Read and write with Read\write, but not with the insert, extract operator, or an error occurs.
Use the EOF () function to detect if a file is read-end, using Gcount () to get the actual number of bytes read
Close File
Use the member function close, such as:
F.close ();
Use destructor
object at the end of its lifetime, the file is checked for shutdown and closed for files that are not closed.
random Read and write file
by moving the file read-write pointer, you can read and write to the file at the 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 location:
Ios::beg = 0 //relative to the file header
Ios::cur = 1 //vs. Current position
Ios::end = 2 //relative to end of file < Span style= "color: #0000ff;" >< Span style= "color: #ff0000;" >< Span style= "color: #008000;" >
Examples of read-write text files
-
//to be able to read the data written to the file correctly, It is best to have separate data between #include <fstream.h>void 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; //display each data cout<<d<<endl; cout<<c<<endl; cout<<s<<endl; f.close () ;}
Operation Result:
1234
3.14
A
How is You
Press any key to continue
Display the contents of a text file
Use Get () to read one character at a time--------------------------------scheme one
-
#include<fstream.h>void Main () { ifstream fin ("d:\\ introduction. txt", ios::nocreate); if (!fin) {cout<<//Note the judgment of the end condition Fin.close ();}
Use Get (char *,int n,char delim= ' \ n ') to read multiple characters at once----scenario two
Clever use of text files without the character ' \ S ' Feature for reading
-
#include<fstream.h>void Main () { ifstream fin ("d:\\ introduction. txt", ios::nocreate); if (!fin) {cout<< while(Fin.get (c,80,//The judgment of the end condition Fin.close ();}
//using read (char *,int N) reading a file---------------------------scenario three
-
#include<fstream.h>void Main () { ifstream fin ("d:\\ introduction. txt", ios::nocreate); if (!fin) {cout<<//Determines whether the file reads end {fin.read (c,80); Cout.write (C,fin.gcount ());} fin.close ();}
Copy files
Binary File Operation example
-
#include<fstream.h>void Main () { ifstream fin ("C:\\1.exe", ios::nocreate|ios::binary); if (!fin) {cout<<return;} ofstream Fout (while(!fin.eof ()) {fin.read (c,1024); Fout.write (c, Fin.gcount ()); } fin.close (); Fout.close (); cout<<"Copy over!\n";
C + + file operations