File stream: Stream with file as input and output object
#include <fstream>
One, file operation open an input or output file
1. Open an output file
Ofstream Fout;
Fout.open ("1.txt");
2. Open an input file
Ifstream fin;
Fin.open ("2.txt");
3. Open a file that can be entered or output
FStream finout;
Finout.open ("3.txt");
More convenient way to Ofstream fout ("1.txt"); Ifstream fin ("2.txt"); FStream finout ("3.txt"); do not specify a path in the default project file
Two. File input, Output
1.>>,<< Way
fout<< "Hello World";
Char ch;
fin>>ch;
operator function input/output
Dec formatted as decimal numeric data input and output
Endl outputs a newline character and refreshes the stream output.
Ends output a null character output
Hex formatted as hexadecimal numeric data input and output
Oct formatted as octal numeric data input and output
setpxecision (int p) Sets the precision bit output of a floating-point number
For example: To use 123 as the hexadecimal output:file1<
2. Function form
1) fout.put (CH) output one character
2) The Get () function is more flexible and has 3 commonly used overloaded forms:
1) fin.get (CH) Take out a character stored in the Into, or ch=fin.get (); The function is to read a character from the stream, the result is saved in the reference CH, if the end of the file is returned, the null character while (Fin.get (CH)) detects whether the end of the file is read
2) ifstream &get (char *buf,int num, ' \ n '); This form reads the character into an array that is pointed to by the BUF until NUM characters are read or the characters specified by Delim are not used, and if you do not use the Delim parameter, the default value will be used line character ' \ n '. For example:
File2.get (str1,127, ' a ');//reads a character from a file to a string str1, terminating when the character ' A ' is encountered or 127 characters are read.
3) Getline:fin.getline (buf,80); Precondition Char buf[80];
Read and write Data blocks
To read and write binary data blocks, use the member function read () and the Write () member functions, which are prototyped as follows:
Read (unsigned char *buf,int num);
Write (const unsigned char *buf,int num);
4) Read () reads num characters from a file into the cache pointed to by buf, and if it is at the end of the file when NUM characters have not been read, you can use the member function int gcount () to get the number of characters actually read, while write () writes NUM characters from the cache pointed to by BUF to the text , it is important to note that the type of cache is unsigned char * and may sometimes require a type conversion.
Cases:
unsigned char str1[]= "I love You";
int n[5];
Ifstream in ("xxx.xxx");
Ofstream out ("yyy.yyy");
Out.write (Str1,strlen (STR1));//write the string str1 all to Yyy.yyy
In.read ((unsigned char*) n,sizeof (n));//Read the specified integer from the xxx.xxx, note the type conversion
In.close (); Out.close ();
Three. Close the file
Fin.close ();
Four. Check if the file is open successfully
if (Fin.fail ())
if (!fin.good ())
if (!fin)
True in parentheses, the open fails
V. Detection of EOF
The member function EOF () is used to detect whether the end of the file is reached, or 0 if the end of the file returns a value other than 0. The prototype is an int eof ();
Example: if (in.eof ()) ShowMessage ("has reached the end of the file!") ");
Vi. random reading of files
SEEKG () The input pointer moves to the specified position, the SEEKP () output pointer moves to the specified position, and with the app, the Seekp method fails.
FIN.SEEKG (Streamoff offset,seek_dir origin);
Offset is generally the number of bytes;
Seek_dir Origin:ios::beg Start position
Ios::cur Current Location
Ios::end End of File
FIN.SEEKG (number of bytes); Move to the position of byte number +1
C + + file stream