C++ file stream fstream detailed description

C++ file stream.
First you need to include the header file #include< fstream >
fstream can be understood as a parent class, and the subclasses included are ifstream and ofstream, etc.
fstream: file input and output class. Indicates the file-level input and output stream (byte stream);
ifstream: file input class. Indicates input from the content of the file, that is, reading the file;
ofstream: file output class. Indicates the file output stream, that is, file writing.
So generally just create a parent class object directly (that is, an object of the fstream class)

The functions of seek() and seekg() in C++
seekg: set the file stream pointer position of the input file stream
seekp: set the file stream pointer position of the output file stream

The C++ file stream detection EOF
member function eof() is used to detect whether the end of the file is reached, and returns a non-zero value if the end of the file is reached, otherwise returns 0. The prototype is inteof();
example: if(in.eof())ShowMessage("The end of the file has been reached!"
)
; One is the read pointer, which indicates the position of the input operation in the file; the other is the write pointer, which indicates the position of the next write operation. Each time an input or output is performed, the corresponding pointer changes automatically. Therefore, the file positioning of C++ is divided into the positioning of the reading position and the writing position. The corresponding member functions are seekg() and seekp(), seekg() is to set the reading position, and seekp is to set the writing position. Their most general form is as follows:
1. istream &seekg(streamoff offset,seek_dir origin);  
2. ostream &seekp(streamoff offset,seek_dir origin);  

Function prototype:
ostream& seekp( streampos pos );
ostream& seekp( streamoff off, ios::seek_dir dir );
istream& seekg( streampos pos );
istream& seekg( streamoff off, ios::seek_dir dir );
The value of dir: the starting position of the search
The dir parameter is used for the positioning operation of the file stream pointer, representing the starting position of the search



seekg(absolute position); //absolute movement, //input stream operation  
seekg(relative position, reference position); //relative operation  
tellg(); //return current pointer position  
seekp(absolute position); //absolute movement, //output stream operation  
seekp(relative position, reference position); //relative operation     
tellp(); //return current pointer position  


The enumeration type defined in ios:
enum seek_dir {beg, cur, end};
the meaning of each enumeration constant:
ios::beg: the starting position of the file stream
ios::cur: the current position of the file stream
ios::end: the end position of the file stream
These two functions are generally used for binary files, because text files may be different from the expected value due to the system's interpretation of characters.
Example:
1. file1.seekg(1234,ios::cur);//Move the read pointer of the file back 1234 bytes from the current position  
2. file2.seekp(1234,ios::beg);//Move the write pointer of the file back 1234 bytes from the beginning of the file 

The code below explains
1: 4 kinds of file read and write operations
2: How to use get(), put(), getline(), write() and read() functions.
3: How to get the size of the file.
4: The essence of matching seekg() and teelg(), seekp() and teelp() are the same

//文件流;
#include<iostream>
#include<fstream>  //ifstream  ofstream
using namespace std;
/*
    No1.文件流流类
    ofstream:打开文件,写文件
    ifstream:打开文件,读操作
    fstream:可读可写
    No2.一般用包含#include<fstream>
    No3.打开文件,关闭文件
    打开文件:
    void open(const char* URL,ios::openmode mode);
    mode: 
        ios:in  读的方式打开文件
        ios:out 写的方式打开文件
        ios::app 追加的方式写文件
        ios::ate  在已有的文件,文件指针在文件末尾
        ios::trunc  文件不存在,创建文件
        ios::binary  二进制形式打开文件,默认方式ASCII码方式打开
        ios::nocreate  不创建的方式
        ios::norepalce  不替换
    组合方式:用的是位或
        ios::in|ios::out can read and write
        ios::out|ios::binary to open the file in binary mode, and
    judge whether the file is opened successfully:
        1.is_open() judges whether the open is successful
        2.! File object Judging whether the file is opened or not is the one that
    closes the file: close() member function closes;
    */
void testOpenFile() { //The function used for testing
    //Opening a file in C++ is a file object, not a file pointer.

    fstream File,File1;
    File.open("2.txt", ios::in | ios::out );
    File1.open("3.txt", ios::in | ios::out | ios::trunc); //
    Using the open function has the same effect as directly using the constructor.
    if (!File.is_open()) {         cerr << "Failed to open file 2.txt!!\n"; }     if     (!File) {         cerr << "Failed to open file 2.txt!!!\n"; }//     This if statement has the same effect as the previous if statement     if (!File1.is_open()) {         cerr << "Failed to open file 3.txt!!\n";     }     else {         cout << "3.txt Open successfully!!!\n";     }     File.close();     File1.close(); } //No2. Read and write method: void asciiReadWriteFile(const char* readFileName,
















    fstream read(readFileName, ios::in);
    fstream write(writeFileName, ios::out);
    //The first reading and writing method: ASCII direct stream reading and writing, >> << /
    *while (1) {         char temp;         read >> temp;         if (read.eof()) {             break;         }         write <<     temp; OK, use get() and put();     /*while (1) {         char temp;         read.get(temp); //temp=read.get(     )         ; same effect!         if (read.eof()) {             break;         }         write.put(temp);     }*/     //The third way of reading and writing: spaces and newlines cannot be reserved, use getline() and write().


















    char str[40]; 
    while (!read.eof()) {
        memset(str, ' ', 40);
        read.getline(str,40);
        int len = strlen(str);
        write.write(str,len);
        write.put('\n');
    }
}
//使用二进制读写方式  使用read()和write()函数来实现
void binaryReadWriteFile(const char* readFile, const char* writeFile) {
    fstream read(readFile, ios::in | ios::binary);
    fstream write(writeFile, ios::out | ios::binary);
    while (!read.eof()) {
        char str[1024]="";
        read.read(str, 1024);
        
        write.write(str, 1024);
    }
    read.close();
    write.close();
}
cur);//The first parameter is positive, indicating the first position counted backward from the current position.     fwrite.put('w');//Append write at the end.















    

}
int main() {
    testOpenFile();
    asciiReadWriteFile("read.txt","write.txt");
    binaryReadWriteFile("read.txt", "binaryTest.txt");
    testSeekReadFile("read.txt");
    return 0;
}
 

Guess you like

Origin blog.csdn.net/qq_20853741/article/details/125024424