Objective:
C + + file stream processing is actually very simple, if you can understand it. The essence of file flow is the use of a buffer middle layer. A bit like standard output and standard input.
The design of C + + IO guarantees IO efficiency while taking into account both encapsulation and ease of use. This article will describe the use of C + + file streams.
Where there are errors and omissions, you are welcome to criticize the evidence.
Header files to include: <fstream>
Namespaces: STD
You can also try <fstream.h>
FStream provides three classes for implementing C + + operations on files. (file creation, read-write).
Ifstream-read from an existing file
Ofstream-Write content to a file
FStream-Open file for read and write
Supported file types
In fact, file types can be divided into two kinds: text files and binary files.
A text file holds a readable character, while a binary file saves only binary data. Using binary mode, you can manipulate images and other files. In text mode, you can only read and write text files. Otherwise you will get an error.
Example one: writing a file
Declaring a Ofstream variable
- Call the Open method so that it is associated with a file
- Write a file
- Call the Close method.
- #include <fstream.h>
- void Main
- {
- Ofstream file;
- File. Open("file.txt");
- file<<"Hello file/n" <<;
- File. Close();
- }
You can try the operator << write content to the file like you tried cout.
Usages:
- file<<"string/n";
- File. put(' C ');
Example two: reading a file
1. Declare a ifstream variable.
2. Open the file.
3. Read data from a file
4. Close the file.
- #include <fstream.h>
- void Main
- {
- Ifstream file;
- Char output[+];
- int x;
- File. Open("file.txt");
- file>>output;
- cout<<output;
- file>>x;
- cout<<x;
- File. Close();
- Similarly, you can use >> to manipulate files like CIN. or call the member function usages:
- File>>char *;
- file>>char;
- File. Get(char);
- File. Get(char *,int);
- File. getline(char *,int sz);
- File. getline(char *,int sz,char eol);
1. Similarly, you can also use the constructor to open a file, you just take the file name as the constructor
The first parameter is available.
- Ofstream file("Fl.txt");
- Ifstream file("Fl.txt");
The Ofstream and Ifstream mentioned above can only be read or written, while FStream provides both read and write functions.
void main()
- {
- FStream file;
- File. Open("File.ext", ISO::in|ios::out)
- Do a input or output here
- File. Close();
- }
The arguments to the open function define the opening mode of the file. A total of the following patterns
- Property List
- iOS:: Inread
- iOS:: OutWrite
- iOS::app starts writing from the end of the file
- iOS::binary binary mode
- iOS::nocreate When you open a file, the file is not created if the file does not exist.
- iOS::noreplace When you open a file, if the file does not exist, create the file
- iOS::trunc Open a file and then empty the content
- iOS::ate when you open a file, move the location to the end of the file
Notes
- The default mode is text
- By default, if the file does not exist, create a new
- Multiple modes can be mixed, with | (Bitwise OR)
- The byte index of the file starts at 0. (just like an array)
We can also call the Read function and the Write function to read and write the file.
The use of the file pointer position in C + +:
- iOS::Beg file Header
- iOS:: EndFile End
- iOS::cur Current Location example:
- File. seekg(0,ios::end);
- int FL_SZ = file. Tellg();
- File. seekg(0,ios::beg);
Common methods of error determination:
- Good() If the file opens successfully
- Bad () An error occurred while opening the file
- EOF() arrives at the end of the file example:
- Char ch;
- Ifstream file("Kool.cpp", iOS::in|ios::out);
- If(file. Good()) cout<<"The file has been opened without problems;
- Else cout<< "An Error had happend on opening the file;
- While(! File. EOF())
- {
- file>>ch;
- cout<<ch;
- }
C + + file stream basic usage (fstream, ifstream, Ostream)