C++檔案讀寫

來源:互聯網
上載者:User

標籤:刪除   lsp   使用   col   i/o   附加   輸入   while   pac   

 
#include <fstream>  ofstream         //檔案寫操作 記憶體寫入存放裝置   ifstream         //檔案讀操作,存放裝置讀區到記憶體中  fstream          //讀寫操作,對開啟的檔案可進行讀寫操作   

 

1.開啟檔案

在fstream類中,成員函數open()實現開啟檔案的操作,從而將資料流和檔案進行關聯,通過ofstream,ifstream,fstream對象進行對檔案的讀寫操作

函數:open()void open ( const char * filename,              ios_base::openmode mode = ios_base::in | ios_base::out );    void open(const wchar_t *_Filename,          ios_base::openmode mode= ios_base::in | ios_base::out,          int prot = ios_base::_Openprot); 

參數: filename   操作檔案名稱

           mode        開啟檔案的方式

           prot         開啟檔案的屬性                            //基本很少用到,在查看資料時,發現有兩種方式

開啟檔案的方式在ios類(所以流式I/O的基類)中定義,有如下幾種方式:

 

ios::in 為輸入(讀)而開啟檔案
ios::out 為輸出(寫)而開啟檔案
ios::ate 初始位置:檔案尾
ios::app 所有輸出附加在檔案末尾
ios::trunc 如果檔案已存在則先刪除該檔案
ios::binary 二進位方式

這些方式是能夠進行組合使用的,以“或”運算(“|”)的方式:例如

ofstream out;  out.open("Hello.txt", ios::in|ios::out|ios::binary)   //根據自己需要進行適當的選取  

開啟檔案的屬性同樣在ios類中也有定義:

0 普通檔案,開啟操作
1 唯讀檔案
2 隱含檔案
4 系統檔案

對於檔案的屬性也可以使用“或”運算和“+”進行組合使用,這裡就不做說明了。

 

C++寫檔案樣本程式:

#include <iostream>#include <string>#include <fstream>using namespace std;//ios_base::trunc - 如果檔案已存在則先刪除該檔案int main(){    string filename = "student.txt";#if 1   //方式一,建立預設檔案輸出資料流對象    ofstream outFile;    outFile.open(filename.c_str(),ios_base::trunc | ios_base::out); //2,以清空寫方式開啟檔案#endif#if 0   //方式二:建立指定檔案和以清空寫文字檔的方式    ofstream outFile(filename.c_str());#endif#if 0   //方式三:建立檔案輸入出流對象,並且以指定清空寫方式打字元檔案    ofstream outFile(filename.c_str(),ios_base::trunc | ios_base::out);#endif    if(outFile.is_open())   //3,判斷檔案是否開啟  成功開啟,返回true    {#if 0        outFile.put(‘C‘);   //4,寫入單個字元        outFile.flush();  //清空緩衝區#endif#if 0        string str = "hello world";        outFile.write(str.c_str(),str.length());    //5,寫入字串        outFile.flush();#endif#if 1        int value = 100;        //6,使用<<,寫入字串        string str = "hello world";        float f = 3.14f;        outFile << value << " " <<str  << " " << f << endl; //6,同時寫入各種不同類型資料        outFile.flush();#endif    }    outFile.close();    //7,關閉檔案}

 

C++讀檔案樣本:

#include <iostream>#include <string>#include <fstream>#include <string.h>using namespace std;//filename.c_str()  c_str()將str轉換成const char *int main(){    string filename = "student.txt";#if 0   //建立預設的ifstream對象    ifstream in;    in.open(filename.c_str(),ios_base::in);#endif#if 0   //建立ifstream對象,並指定到某一個檔案,預設開啟字元檔案    ifstream in(filename.c_str());#endif    //建立ifstream對象,並指定到某一個檔案,並以指定方式開啟檔案    ifstream in(filename.c_str(),ios_base::in);    char buf[128] = {0};    if(in.is_open())    {#if 0        char ch = in.get();     //讀一個字元        cout << "ch = " << ch << endl;#endif#if 0        memset(buf,0,sizeof(buf));        in.getline(buf,sizeof(buf) - 1);    //只能讀一行        cout << buf << endl;#endif#if 1        memset(buf,0,sizeof(buf));        in.read(buf,sizeof(buf) - 1);       //可以從緩衝區讀取,也能從檔案中直接讀取        cout << buf << endl;#endif#if 0        cout << "in.gcount() = " << in.gcount() <<  endl;       //gcount用於檢查緩衝區內可讀位元組數        memset(buf,0,sizeof(buf));        in.read(buf,sizeof(buf) - 1);        cout << "in.gcount() = " << in.gcount() <<  endl;        in.readsome(buf,in.gcount());   //只能從緩衝區讀取內容        cout << buf << endl;#endif#if 0        //file: 100 hello world 3.14        string str1,str2,str3;        in >> str1 >> str2 >> str3;     //使用>>每次從檔案讀一個單詞,以空格或‘\n‘為分隔字元        cout << str1 << endl;   //100        cout << str2 << endl;   //hello        cout << str3 << endl;   //world#endif#if 0        char ch;        while(!in.eof())        //eof()函數用於檢查是否到達檔案結尾        {            ch = in.get();            cout << ch;        }#endif#if 0        while(!in.eof())        {            memset(buf,0,sizeof(buf));            in.getline(buf,sizeof(buf) - 1);    //讀取一行,但是不會讀分行符號            cout << buf << endl;        }#endif#if 0        while(!in.eof())        {            memset(buf,0,sizeof(buf));            in.read(buf,sizeof(buf) - 1);            cout << buf;        }        cout << endl;#endif#if 0    string str;    while(!in.eof())    {        in >> str;        cout << str << endl;    }#endif    in.close();    }}

 


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.