C++對文件的操做

1. 文件寫入:ios

#include <fstream>
using namespace std;

void main()
{
    ofstream out;    //建立一個文件輸出流
    out.open("C:\\123.txt");    //輸出到文件
    out << "鋤禾日當午,汗滴禾下土。" << endl;
    out.close();


    system("C:\\123.txt");
}

2. 文件讀取:app

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

void main()
{
    ifstream in;    //建立一個文件輸出流
    in.open("C:\\123.txt");    //從文件讀取

    char str[256]{ 0 };
    //in >> str;
    in.getline(str, 256);//處理空格

    in.close();

    cout << str << endl;

    cin.get();
}

3. 文件追加:spa

#include <fstream>
using namespace std;

void main()
{
    ofstream out;    //建立一個文件輸出流
    out.open("C:\\123.txt",ios::app);    //追加方式寫入文件
    out << "誰知盤中餐,粒粒皆辛苦。" << endl;
    out.close();


    system("C:\\123.txt");
}

    

4. 文本文件讀寫:指針

#include <iostream>
#include <fstream>

using namespace std;

struct info
{
    char name[10];
    int id;
    double price;
};

void main()
{
    struct info infs[3] = { {"xiaohua",99,5000},{"xiaohong",89,4000},{"xiaoli",79,3000} };
    //ofstream fout("C:\\3-文件操做練習\\1.txt",ios::out|ios::app);
    ofstream fout("C:\\3-文件操做練習\\1.txt");
    for (auto i : infs)
    {
        fout << i.name << " " << i.price << " " << i.id << endl;
    }
    fout.close();

    ifstream fin("C:\\3-文件操做練習\\1.txt");
    for (int i = 0; i < 3; i++)
    {
        char str[255]{ 0 };
        fin.getline(str, 254);
        cout << str << endl;
    }
    fin.close();

    cin.get();
}

5. 二進制文件讀寫:code

#include <iostream>
#include <fstream>

using namespace std;

struct info
{
    char name[10];
    int id;
    double price;
};

void main()
{
    struct info infs[3] = { { "xiaohua",99,5000 },{ "xiaohong",89,4000 },{ "xiaoli",79,3000 } };
    ofstream fout("C:\\3-文件操做練習\\2.bin",ios::binary);
    fout.write((char *)infs, sizeof(infs));    //從內存寫入磁盤
    fout.close();

    struct info infshua[3]{ 0 };
    ifstream fin("C:\\3-文件操做練習\\2.bin", ios::binary);
    fin.read((char *)infshua, sizeof(infshua));
    fin.close();

    for (auto i : infshua)
    {
        cout << i.name << " " << i.price << " " << i.id << endl;
    }

    cin.get();
}

6. 文件指針移動:blog

  (1)移動到合適位置,讀:內存

#include <iostream>
#include <fstream>

using namespace std;

void main()
{
    ofstream fout("C:\\3-文件操做練習\\3.txt");
    if (!fout)
    {
        cout << "文件操做失敗!\n" << endl;
    }
    fout << "123456789abcdefghijklmnopqrstuvwxyz" ;
    fout.close();

    ifstream fin("C:\\3-文件操做練習\\3.txt");
    if (fin.fail())
    {
        cout << "文件操做失敗!\n" << endl;
    }
    fin.seekg(9, ios::beg);//文件指針從開始移動9個位置 讀
    char ch;
    while (fin.get(ch))
    {
        cout << ch;
    }
    fin.close();


    cin.get();
}

  (2)移動到合適位置,寫:ci

#include <iostream>
#include <fstream>

using namespace std;

void main()
{
    ifstream fin("C:\\3-文件操做練習\\3.txt");
    if (fin.fail())
    {
        cout << "文件操做失敗!\n" << endl;
    }
    char ch;
    while (fin.get(ch))
    {
        cout << ch;
    }
    fin.close();

    ofstream fout("C:\\3-文件操做練習\\3.txt");
    if (!fout)
    {
        cout << "文件操做失敗!\n" << endl;
    }
    fout.seekp(5, ios::beg);
    fout << "hello world" << endl;
    fout.close();

    cin.get();
}
相關文章
相關標籤/搜索