c_cpp 文件IO(.txt文件)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 文件IO(.txt文件)相关的知识,希望对你有一定的参考价值。

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    // Writing to a file
    ofstream outFile;
    outFile.open("Test.txt");
    outFile << "This is a test from CLion!" << endl;
    outFile.close();

    // Reading from a file
    ifstream inFile;
    inFile.open("Test.txt");
    if (inFile.is_open())
    {
        // Move the cursor of the file to the end of the file
        inFile.seekg(0, std::ifstream::end);

        // Give the position of the current character
        int length = static_cast<int>(inFile.tellg());

        // Move cursor to the start of the file
        inFile.seekg(0, std::ifstream::beg);

        // Output buffer
        char *buffer = new char[length + 1];
        inFile.get(buffer, length);

        cout << buffer << endl;

        delete[] buffer;

        inFile.close();
    }

    return 0;
}

以上是关于c_cpp 文件IO(.txt文件)的主要内容,如果未能解决你的问题,请参考以下文章

java读取txt文件,对字符串进行操作后导出txt文件

java中从txt文件读取数据以及写数据到txt文件

Java读取txt文件和写入txt文件

IO流一行一行读取TXT文件

生成和解析txt文件

文件IO(存取.txt文件)