Input/output stream
The C + + input-output stream is an IO system that provides users with a unified interface that isolates specific device differences.
Includes standard input and output streams and file streams.
Standard input/output stream
IStream is an input stream class, and CIN is an object of type IStream.
Ostream is an output stream class and cout is an object of type Ostream.
In C + +, users are allowed to overload the operator "<<" and ">>" to implement object input and output.
When you overload these two operators, the function that overloads the two operators describes the friend function of the class in the class where the object resides.
C + + in the header file fstream.h defines the C + + file Flow class system , when using files in the program, to include the header file fstream.h.
It defines the various file manipulation operators and functions.
When the operation involves a text file, the input file is treated as a keyboard, and the output file is treated as a monitor with the same format. Just add the statement that opens and closes the file in the program.
files can be divided into two main categories: text files, binary files.
Defining File Objects
There are three types of objects:
FStream file;//is both readable and writable.
Ifstream infile;//can be read and not writable.
Ofstream outfile;//can be written and unreadable.
Open File
Ifstream infile;
Infile.open ("C:\\myfile.txt"); associating with a file
Ofstream outfile;
Outfile.open ("Myoutfile.txt");
Read and write files
std::string str;
The infile>>str;//is entered from the file stream.
outfile<<str;
Close File
Infile.close ();
Outfile.close ();
Releases the file object after closing and cancels the association with the file.
After you define the FStream object, you can read and write the file. Therefore, you need to give the open mode when opening:
FStream file1,file2;//defines two objects for a file class
File1.open ("File1.txt", ios::in);//For input
Pile2.open ("File2.txt", ios::out);//For Output
Open often fails because the file does not exist or is being occupied.
You can tell whether a file object is open by judging it.
File1.open ("file.txt", ios::in);
if (!file1)
{
File open failed.
}
There is a convention:
When the file is opened for reading, the open fails if the file does not exist.
When writing, create a new file if the file does not exist. Delete the contents of the original file if it exists.
The text file differs from the binary file Read method:
Read and write text files
Reads a character from a file: File.getch (CH);
Reads a row of data: File.getline (str);
When reading from a file, when the end of the file is read, the function returns a value of 0, which can be used to determine the end of the loop.
Writes a character to a file: File.put (CH);
Instance for two copies of files
To open a binary file
If there is no special description in the file's open method, the default open text file, to open the binary file, you want to specify.
FStream Infile,outfile;
Infile.open ("Inf1.dat", ios::in| ios::binary);
Outfile.open ("Outf1.dat", ios::out| ios::binary);
Ios::out and ios::in represent IO mode. Enumeration type, multiple enumeration values can use the operator ' | ' Combined use.
Closing of binary files
The closing method is the same, calling the Close method.
Read and write binary files
Binary files are read and written differently from text files, using a specialized method.
Infile.read (char *buff, int nnum);
The buff stores the buffer of the data, nnum the length to be read, and returns the actual length of the data read.
Infile.read (&stuinfo, sizeof (Stuinfo));
Infile.read (Pbuff, 1024*sizeof (char));
To write data to a file:
Outfile.write (Pbuff, Nlen);
Write returns the length of the data actually written.
The end of a binary file
Infile.eof ()
When the end of the file is reached, the function returns a value other than 0, otherwise zero is returned.
File pointers
After the file is opened, after reading or writing a piece of data, the file pointer moves to the end of the data read or written.
such as File.read (Pbuff, 1024);
After the read succeeds, the file pointer is shifted backwards by 1024 bytes.
File.write (Pbuff, 10);
After the write succeeds, the file pointer is offset backwards by 10 bytes.
Read and Write methods can automatically move the file pointer, in addition to a special function can be a random move of the file pointer.
INFILE.SEEKG (Nnum, ios::cur);
Nnum is the number of bytes moved.
Ios::cur represents the current position. There are two other relative positions: Ios::begin, ios::end;
INFILE.SEEKG (Ios::begin);//relative files begin to move the file pointer by 10 bytes.
INFILE.SEEKG ( -20, ios::end);//Move forward 20 bytes relative to the end of the file
Instance to implement a copy of two binaries.
C + + Note (9): IO input/output stream