C++對檔案的操作

來源:互聯網
上載者:User

標籤:code   失敗   line   fst   auto   二進位   ++   開始   using   

1. 檔案寫入:

#include <fstream>using namespace std;void main(){    ofstream out;    //建立一個檔案輸出資料流    out.open("C:\\123.txt");    //輸出到檔案    out << "鋤禾日當午,汗滴禾下土。" << endl;    out.close();    system("C:\\123.txt");}

2. 檔案讀取:

#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. 檔案追加:

#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. 二進位檔案讀寫:

#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. 檔案指標移動:

  (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)移動到合適位置,寫:

#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();}

 

C++對檔案的操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.