C++ fstream – 文件操作

在 C++ 中,fstream 是用於進行文件操作的一組流類。具體來說,它可以用來讀取、寫入、修改文件內容,甚至可以在二進位文件中進行讀寫操作。本文將從多個方面對 fstream 進行詳細的闡述。

一、c++ fstream 寫文件

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    fstream file("example.txt", ios::out);
    if (!file.is_open())
    {
        cout << "Unable to open file";
        return 0;
    }
    file << "Hello, world!";
    file.close();
    return 0;
}

以上代碼演示了如何創建一個文件,打開它並寫入內容。在這裡,ios::out 是指以寫入模式打開文件,若文件存在則清空,若不存在則新建。可以使用 os::app 進行追加模式的寫入。

二、fstream讀文件

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    string line;
    fstream file("example.txt", ios::in);
    if (!file.is_open())
    {
        cout << "Unable to open file";
        return 0;
    }
    while (getline(file, line))
    {
        cout << line << endl;
    }
    file.close();
    return 0;
}

以上代碼演示了如何打開文件並讀取它的內容。在這裡,ios::in 是指以讀取模式打開文件。我們可以用 getline 函數逐行讀取文件內容,並將其輸出。

三、fstream寫文件

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    fstream file("example.txt", ios::out);
    if (!file.is_open())
    {
        cout << "Unable to open file";
        return 0;
    }
    file << "Hello, world!";
    file.close();
    return 0;
}

以上代碼演示了如何打開文件並寫入內容。在這裡,我們同樣使用 ios::out 模式來打開文件。

四、fstream判斷文件結尾

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    char c;
    fstream file("example.txt", ios::in);
    if (!file.is_open())
    {
        cout << "Unable to open file";
        return 0;
    }
    while (!file.eof())
    {
        file.get(c);
        cout << c;
    }
    file.close();
    return 0;
}

以上代碼演示了如何在讀取文件時判斷文件結尾。在這裡,我們使用了 eof 函數來判斷文件是否已到達結尾。

五、fstream讀寫文件

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    fstream file("example.txt", ios::in | ios::out);
    if (!file.is_open())
    {
        cout << "Unable to open file";
        return 0;
    }
    string line;
    while (getline(file, line))
    {
        cout << line << endl;
    }
    file << "This line is added later.";
    file.close();
    return 0;
}

以上代碼演示了如何在同一個文件中進行讀寫操作。這裡我們使用了 ios::in | ios::out 模式來打開文件。

六、fstream修改文件內容

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
    fstream file("example.txt", ios::in | ios::out);
    if (!file.is_open())
    {
        cout << "Unable to open file";
        return 0;
    }
    string line;
    getline(file, line);
    size_t pos = line.find("world");
    if (pos != string::npos)
    {
        line.replace(pos, 5, "C++ world");
    }
    file.seekp(0, ios::beg);
    file << line;
    file.close();
    return 0;
}

以上代碼演示了如何修改文件中的一行內容。在這裡,我們使用了字元串的 find 和 replace 函數來搜索和替換文件內容,同時,使用 seekp 函數把文件指針移動到文件開頭。

七、fstream讀寫二進位文件

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
struct Person
{
    char name[20];
    int age;
    double height;
};
int main()
{
    fstream file("example.bin", ios::in | ios::out | ios::binary);
    if (!file.is_open())
    {
        cout << "Unable to open file";
        return 0;
    }
    Person p;
    file.read((char*)&p, sizeof(p));
    cout << "Name: " << p.name << endl;
    cout << "Age: " << p.age << endl;
    cout << "Height: " << p.height << endl;
    p.age = 30;
    file.seekp(0);
    file.write((char*)&p, sizeof(p));
    file.close();
    return 0;
}

以上代碼演示了如何讀寫二進位文件。在這裡,我們定義了一個結構體,然後使用 read 和 write 函數來讀寫文件內容。需要注意的是,我們要使用 ios::binary 模式打開文件,以保證進行二進位讀寫。

八、fstream清空文件

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    fstream file("example.txt", ios::out);
    if (!file.is_open())
    {
        cout << "Unable to open file";
        return 0;
    }
    file << ""; // clear file
    file.close();
    return 0;
}

以上代碼演示了如何清空文件。在這裡,我們把文件指針移動到文件開頭,並寫入空字元串,即可清空文件內容。

九、fstream創建文件

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    fstream file("example.txt", ios::out | ios::trunc);
    if (!file.is_open())
    {
        cout << "Unable to create file";
        return 0;
    }
    file << "This is a newly created file.";
    file.close();
    return 0;
}

以上代碼演示了如何創建一個新文件。在這裡,我們使用了 ios::out | ios::trunc 模式來打開文件,這表示如果文件存在則清空其內容,否則新建文件。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/256409.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-15 12:40
下一篇 2024-12-15 12:40

相關推薦

發表回復

登錄後才能評論