Basic usage of c ++ file stream fstream and ifstream, fstreamifstream
The processing of c ++ file streams is actually very simple, provided that you can understand it. In essence, a file stream uses a buffer intermediate layer. A bit similar to standard output and standard input.
The Design of c ++ IO ensures IO efficiency while taking into account both encapsulation and ease of use. This article describes how to use the c ++ file stream.
Where there are errors and omissions, you are welcome to criticize and testify.
Header files to be included:
Namespace: std
You can also try it out.
Fstream provides three classes for c ++ to perform file operations. (File Creation, read/write ).
Ifstream -- read from an existing file
Ofstream -- write content to a file
Fstream-open a file for reading and writing
Supported file types
In fact, there are two types of files: text files and binary files.
Text Files store readable characters, while binary files only store binary data. In binary mode, you can operate images and other files. In text mode, you can only read and write text files. Otherwise, an error is reported.
Example 1: Write a file
Declare An ostream variable
- Call the open Method to associate it with a file
- Write files
- Call the close method.
#include
#include
using namespace std;int main(){ofstream file;file.open("file.txt");file<<"Hello file/n"<<75;file<<"string/n";file.put('c');file.close();}
You can try the operator <write content to a file as you try cout.
Usages:
file<<"string/n";file.put('c');
Example 2: Read File 1. Declare An ifstream variable. 2. Open the file. 3. read data from the file. 4. close the file.
# Include
# Include
Using namespace std; int main () {ifstream file; char output [100]; char x; file. open ("file.txt", ios: in | ios: out); // file> output; // cout <
> X; cout <
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 use the constructor to open a file. You only need to use the file name as the constructor's
The first parameter is enough.
ofstream file("fl.txt");ifstream file("fl.txt");
The ofstream and ifstream mentioned above can only be read or written, while fstream also provides the read/write function.
Void main ()
{fstream file; file.open("file.ext",ios::in|ios::out) //do an input or output here file.close();}
The open function parameter defines the open mode of the file. There are a total of the following modes:
Attribute list ios: in read ios: out write ios: app write from the end of the file ios: binary mode ios: nocreate when opening a file, if the file does not exist, do not create files. Ios: noreplace: When opening a file, if the file does not exist, create the file ios: trunc to open a file, and then clear the content ios: ate when opening a file, move the location to the end of the file
Notes
- The default mode is text.
- If the file does not exist by default, a new
- Multiple modes can be mixed. | (by bit or)
- The byte index of the file starts from 0. (Just like an array)
We can also call the read and write Functions to read and write files.
Usage of file pointer position in c ++:
Ios: beg File Header ios: end file tail ios: cur current location example:
- File. seekg (0, ios: end );
-
- Int fl_sz = file. tellg ();
-
- File. seekg (0, ios: beg );
# Include
# Include
Using namespace std; int main () {char ch; ifstream file ("kool. cpp ", ios: in | ios: out); if (file. good () {cout <"The file has been opened without problem";} else cout <"An error has happened on opening the file"; while (! File. eof () // output {file> ch; cout <
Common error determination methods:
- Good () if the file is successfully opened
- An error occurred while opening the file in bad ().
- Example of eof () arriving at the end of a file: