In C , there is a stream class, all I / O is based on this "stream" class, including the file I / O we have to know, and Stream has two important operators: STREAM:
1. Insert (<<) outputs data to stream. For example, the system has a default standard output stream (COUT), in general, the display, so cout << "Write stdout" << '/ n'; "Write Stdout" and wrap characters ('/ n') outputs to standard output flow.
2, the parser (>>) inputs data from the stream. For example, the system has a default standard input stream (CIN). In general, the keyboard is referred to, so CIN >> X; represents data that reads a specified type (ie, the type of variable x) from the standard input stream .
In C , the operation of the file is implemented by Stream's sub-FSTREAM (File Stream), so use this way to operate files, you must join the header file FStream.h. Here, this type of file operation process will come.
First, open the file in the FSTREAM class, there is a member function Open (), which is used to open the file, its prototype is:
Void Open (Const Char * FileName, INT MODE, INT Access);
parameter:
Filename: The file name to open: Open the file to open the file Access: Open the file's properties Open the file in the way in the class iOS (is a base class of all stream I / O classes), the common value is as follows:
iOS :: App: Open files in appended way iOS :: ATE: The file is turned to the file end, iOS: app contains this property ios :: binary: Open file in binary, the default method is text . The difference between the two ways, IOS :: in: file open iOS :: OUT: file in an input method Open iOS :: NOCREATE: No file, so the file does not exist when the failed IOS :: NorePlace: Not overwritten File, so if the file is open if the file is failed iOS :: trunc: If the file exists, set the file length to 0 to connect the above properties, such as ios :: Out | os :: binary
Opens the property of the file is:
0: Ordinary file, open access 1: Read-only file 2: Include file 4: System files can be connected with "or" or " " to connect the above properties, such as 3 or 1 | 2 is read-only and implied attributes open a file.
For example: Open files in binary inputs C: /config.sys
FSTREAM FILE1; File1.Open ("C: //config.sys", ios :: binary | ios :: IN, 0);
If the Open function only has a file name, the Open / write ordinary file is opened, namely:
File1.Open ("c: //config.sys"); <=> file1.Open ("c: //config.sys", ios :: in | os :: out, 0);
In addition, FSTREAM also has the same constructor as Open (), for the above example, can open the file when defined: fstream file1 ("c: //config.sys");
In particular, FStream has two subcategories: IFStream (Input File Stream) and OFSTream (Output File Stream), ifstream defaults to open files in an input mode, and the OFSTream is open in an output mode by default.
Ifstream file2 ("c: //pdos.def"); // Open file OFSTREAM file3 ("C: //x.123") in an input mode; // Open file in output
Therefore, in practical applications, select different classes as needed: If you want to open in an input mode, use ifstream to define; if you want to open it in an output, use OFStream to define; if you want to input / Open the output, use FStream to define.
Second, the file opened by the shutdown file must be turned off after using the file, and FStream provides the member function close () to complete this, such as file1.close (); turn the file1 connected file.
Third, read and write file read and write files are divided into text files and binary files. For text files, it is relatively simple, and it is possible to use inserts and parsers; and for binary reading, it is complex. Introduce these two ways under detail
1. Reading and writing text files for text files is very simple: use the insert (<<) to output to the file; use the quote (>>) from the file input. Suppose File1 is opened in an input, and FILE2 is turned on. Examples are as follows:
File2 << "i love you"; // write a string "I love you" INT i; file1 >> i; // from the file into the file.
This approach has a simple formatting ability, such as the specified output is 16, and the specific format has some
Moderate function input / output DEC formatter is decimal numerical data input and output ENDL output a newline character and refreshed this stream output ENDS output an empty character output HEX formatted to hexadecimal numerical data input and output OCT formatted as an octal Numerical data input and output setpxecision (int P) set the precision bits output of floating point numbers
For example, as you want to use 123 as a hexadecimal output: file1 << HEX << 123; 3.1415926 to 5-bit accuracy output: file1 << setpxecision (5) << 3.1415926.
2, the reading and writing of the binary file 1Put () PUT () function writes a character to the stream, whose prototype is OFSTream & Put (Char CH), is also simple to use, such as file1.put ('c'); A character 'c'.
2Get () get () function is flexible, there are three commonly used overload forms:
One is the form of PUT (): ifstream & Get (CHAR & Ch); the function is to read a character from the stream, and the result is saved in the reference CH, if the file is tail, return empty characters. Such as file2.get (x); indicates that a character is read from the file and saves the read characters in X.
Another prototype of the overloaded form is: int GET (); this form is to return a character from the stream, if the file is reached, returns EOF, such as x = file2.get (); and the above example function is the same . There is also a form of prototype: ifstream & get (char * buf, int num, char delim = '/ n'); this form reads characters into arrays pointed to by BUF until the NUM characters are read or encountered. The characters specified by Delim, if Delim is not used, the default quotation '/ n' will be used. E.g:
FILE2.GET (STR1, 127, 'A'); // Read characters from the file to the string STR1, and terminate when character 'a' is encountered or read 127 characters.
3 Read and write data blocks To read and write binary data blocks, use member functions read () and write () member functions, their prototypes are as follows:
Read (Unsigned Char * BUF, INT NUM); Write (Const unsigned char * buf, int num);
Read () Reads NUM characters from the file to the cache pointing to the BUF. If you go to the end of the file when you have not read NUM characters, you can use the member function Int gcount (); to get the actual number of characters Write () writes NUM characters from the BUF to the NUM character to the file, it is worth noting that the type of cache is unsigned char *, sometimes the type conversion may be required.
example:
Unsigned char str1 [] = "i love you"; int N [5]; ifstream in ("xxx.xxx"); OFSTREAM OUT ("YYY.YYYYY"); Out.write (str1, str1)); / / Write the string str1 all in YYY.YYY in. Read ((unsigned char *) n, sizeof (n)); // Read the specified integer from XXX.xxx, pay attention to the type conversion in. Close Out.close ();
Fourth, the detection EOF member function EOF () is used to detect whether to reach the file, if the end of the file returns a non-0 value, otherwise returns 0. The prototype is int EOF ();
Example: if (in.eof ()) showMessage ("already reaches the file tail!");
V. File location and C file operation, the C I / O system manages two pointers associated with a file. One is a read pointer, which indicates that the input is operated in the file; the other is the write pointer, which is the next time the next time. The corresponding pointer automatically changes each time the input or output is performed. Therefore, the C file position is divided into the positioning of the read position and the write position. The corresponding member function is SEEKG () and seekp (), and seekg () is setting the read position, and the Seekp is set. Their most common form is as follows:
iStream & Seekg (Streamoff Offset, Seek_Dir Origin); Ostream & Seekp (SEEK_DIR Origin);
Streamoff is defined in iostream.h, defines the maximum value that the offset OFFSET can achieve, seek_dir represents the movement of the baseline, an enumeration with the following value:
iOS :: beg: Files ios :: Cur: File Current Position iOS :: End: File End These two functions are generally used for binary, because the text files may differ from the expected value because the text files can be different from the interpretation of the character. example:
File1.seekg (1234, iOS :: Cur); // Put the read pointer of the file from the current location 1234 bytes file2.seekp (1234, ios :: beg); // Put the file from the file 334 bytes at the beginning