c++ file operation - text file - read and write files

Table of contents

1. Three types of file operations: (file stream)

2. How to open the file:

3. Text file--steps to write a file:

Example 1: Writing to a file with ios::out

 4. Text file--steps to read the file:

Example 2: Use ios::in to read the contents of the file

Recently, I have sorted out the knowledge points of c++, and record them here.

Why learn file operations:

The data generated by running the program is temporary data. Once the program finishes running, the data will be released, and the data can be persisted through files.

  1. C++ file operations need to include the header file <fstream>.
  2. There are two file types:

Text file: ASCII code is stored in the text file, and the content of the file can be directly input and output;

Binary files: Binary files directly store characters, and the contents of binary files cannot be output directly to the screen.

1. Three types of file operations: (file stream)

ofstream write operation, o stands for output, f stands for file

ifstream read operation, i stands for input, f stands for file

fstream read and write operations

2. How to open the file:

ios :: in

read only open

ios :: out

write only open

ios :: ate

open at the end of the file (ate: at the end)

ios :: app

write file append

ios :: trunk

If the file is deleted first and then opened after creation

ios :: binary

Manipulate files in binary mode

Note: The file opening method can be used in conjunction with the "|" (or) operator.

For example: write files in binary mode: ios::binary | ios::out

3. Text file--steps to write a file:

  • Include header files: #include<fstream>;
  • Create a stream object: ofstream ofs;
  • Open the file: ofs.open("file path", open method);
  • Write data: ofs<<"written data";
  • Close the file: ofs.close();

Example 1: Writing to a file with ios::out

#include <iostream>
#include <fstream>

using namespace std;


//文本文件——写文件
void test1()
{
    //1.包含头文件

    //2.创建流对象
    ofstream ofs;

    //3.指定打开方式
    ofs.open("daidia.txt",ios::out);//此处不指定文件生成路径,默认在当前工程文件夹下

    //4.写内容
    ofs << "日期:2023.04-25" << endl;
    ofs << "天气:阴" << endl;
    ofs << "工作:学习c++" << endl;

    //5.关闭文件
    ofs.close();

}


int main()
{
    test1();
    
}

Effect: Find the project folder path and you can see the daidia.txt file. Open:

 4. Text file--steps to read the file:

  • Include header files: #include<fstream>;
  • Create a stream object: ifstream ifs;
  • Open the file: ifs.open("file path", open method);
  • Read data: read data in four ways
  • Close the file: ifs.close();

Example 2: Use ios::in to read the contents of the file

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//文本文件——读文件
void test2()
{
    //1.包含头文件

    //2.创建流对象
    ifstream ifs;

    //3.打开文件,判断文件是否打开成功
    ifs.open("daidia.txt", ios::in);
    if (!ifs.is_open())
    {
        cout << "文件打开失败" << endl;
        return;
    }

    //4.读文件内容
    //第一种
    char buff[1024] = { 0 };
    while (ifs>>buff)
    {
        cout << buff << endl;
    }

    //第二种
    /*char buff[1024] = {0};
    while (ifs.getline(buff, sizeof(buff)))
    {
        cout << buff << endl;
    }*/

    //第三种
    /*string buff;
    while (getline(ifs, buff))  //如果此处使用getline报错,头文件加入#include <string>
    {
        cout << buff << endl;
    }*/

    //第四种,效率低
    /*char c;
    while ((c = ifs.get()) != EOF)//EOF:end of file,循环判断是否读到文件尾,没读到一直读
    {
        cout << c ;
    }*/



    //5.关闭文件
    ifs.close();

}


int main()
{
    //test1();
    test2();
}

Guess you like

Origin blog.csdn.net/weixin_55735677/article/details/130361433