C + + file flow
In C + +, there is a stream this class, all I/O is based on this "flow" class, including the file we want to know i/o,stream this class has two important operators:
Output data to the stream. For example, the system has a default standard output stream (cout), usually refers to the display, so,cout<< "write Stdout" << ' n ', which means the string "write Stdout" and newline characters (' n ') Output to the standard output stream.
Enter data from the stream. For example, the system has a default standard input stream (CIN), which is generally referred to as the keyboard, so,cin>>x; represents data from the standard input stream that reads a specified type (that is, the type of the variable x).
In C + +, the operation of the file is implemented through the subclass FStream (file stream) of the stream, so to manipulate the file in this way, you must join the header file fstream.h. The following is the operation of this type of file one by one-way.
First, open the file
In the FStream class, there is a member function open (), which is used to open the file, and its prototype is:
Parameters:
FileName: the filename to open
Mode: How to open a file
Access: Open a file's properties
The way you open the file is defined in class iOS (the base class for all streaming I/O classes), and the common values are as follows:
Ios::app: Open the file as an append
Ios::ate: When the file is opened and positioned at the end of the file, Ios:app contains this property
Ios::binary: Opens the file in binary mode, the default way is the text mode. The difference between the two ways is seen in the preceding text
Ios::in: File opened in input mode
Ios::out: File opened in output mode
Ios::nocreate: Failed to create file, so open file does not exist
Ios::noreplace: File is not overwritten, so if the file fails when the file is opened
The properties of the open file are:
0: Normal file, open access
1: Read-only files
2: Implied file
4: System files
You can connect the above attributes with "or" or "+", such as 3 or 1|2 to open the file with read-only and suppressed attributes.
If the open function has only one argument for the file name, it is opened with a read/write normal file, that is:
In addition, FStream has the same constructor as open (), and for the above example, the file can be opened at the time of the definition:
Specifically, the FStream has two subclasses: ifstream (input file stream) and Ofstream (OUTPU file stream), Ifstream opens the file by default as input, Instead, the Ofstream opens the file by default in output mode.
Ifstream file2 ("C:pdos.def");//Open file as input
Ofstream file3 ("c:x.123");//Open File as Output
So, in practical applications, depending on the needs, choose a different class to define: If you want to open it as input, use Ifstream to define it, and if you want to open it as output, use Ofstream to define it, and if you want to open it in the input/output mode, use FStream to define it.
Second, close the document
The open files must be closed after use, FStream provides the member function close () to complete this operation, such as: File1.close (), and the file1 connected file is closed.
Iii. Reading and writing documents
Read and write files are divided into text files and binary file reading, for text file reading is relatively simple, with the insertion and the extraction device can be, and for binary read more complex, the next to the detailed introduction of the two ways
1. Reading and writing of text files
The reading and writing of a text file is simple: output to the file with the insertion device (<<), and input from the file with the extract (>>). Suppose File1 is opened as input, and file2 is opened with output. Examples are as follows:
file1>>i;//Enter an integer value from the file.
This approach also has a simple format, such as the ability to specify the output of 16, and so on, the specific format of the following
operator function input/output
Dec formatted as decimal numeric data input and output
Endl output a newline character and refreshes the output of this stream.
Ends output an empty character output
Hex formatted as hexadecimal numeric data input and output
Oct format to octal numeric data input and output
setpxecision (int p) sets the precision digit output of a floating-point number
For example, to use 123 as the hexadecimal output:file1<
2. Read and write binary files
The put () function writes a character to the stream, and its prototype is Ofstream &put (char ch), which is also simpler to use, such as File1.put (' C '), or write a character ' C ' to the stream.
The Get () function is more flexible and has 3 commonly used overloaded forms:
One is the form that corresponds to put (): Ifstream &get (char &ch); The function is to read a character from the stream, the result is saved in reference CH, and if the end of the file, the null character is returned. such as File2.get (x), which means reading a character from a file and Fu Paocun the read word in X.
Another type of overloaded form is an int get (), which returns a character from the stream, and if the end of the file is reached, EOF is returned, such as X=file2.get (), and the previous example features are the same.
Another form of the prototype is: Ifstream &get (char *buf,int num,char delim= ' n '), which reads the character into the array pointed to by BUF until the NUM character is read or the character specified by the Delim is encountered, if it is not used Delim This parameter, the default value is used to line up the ' n '. For example:
File2.get (str1,127, ' a '); reads the character from the file to the string str1, terminates when the character ' A ' is encountered or 127 characters are read.
③ read and write data blocks
To read and write binary blocks of data, use the member function read () and write () member functions, which are prototypes as follows:
Read () reads num characters from a file into the cache pointed to by BUF. If the end of the file has not yet been read into the NUM character, the member function int gcount () can be used to get the number of characters actually read, and write () writes NUM characters from the cache pointed to by BUF to the file , it is worth noting that the type of cache is unsigned char * and may sometimes require type conversions.
Cases:
Out.write (Str1,strlen (STR1))//write String str1 all to Yyy.yyy
In.read ((unsigned char*) n,sizeof (n));//Read the specified integer from the xxx.xxx, note type conversion
V. Positioning of documents
Unlike the C file operation, the C + + I/O system manages two pointers associated with a file. One is the read pointer, which shows the position of the input operation in the file, and the other is the write pointer, the location of the next write operation. Each time the input or output is executed, the corresponding pointer changes automatically. Therefore, the file location of C + + is divided into the location of read and write location, the corresponding member function is SEEKG () and SEEKP (), SEEKG () is set the read location, SEEKP is set write location. Their most common form is as follows:
Streamoff is defined in iostream.h, defines the maximum value that offset offset can take, Seek_dir represents the base position of the move, and is an enumeration with the following values:
Ios::beg: Beginning of File
Ios::cur: File Current Location
Ios::end: End of File
These two functions are typically used in binary files because the text file may be different from the expected value because of the system's interpretation of the character.
Cases:
FILE1.SEEKG (1234,ios::cur)//Move the read pointer of the file back 1234 bytes from the current position
FILE2.SEEKP (1234,ios::beg)//Move the file's write pointer back 1234 bytes from the beginning of the file
If VC programming, it is better to use CFile class, etc. more convenient for file operations </CA>