c++檔案讀寫

來源:互聯網
上載者:User

 

http://stlchina.huhoo.net/bin/view.pl/Main/STLDetailString 

 

添加#include
<fstream>

using namespace std;

View Code

 1 //    ofstream myfile("c:\\1.txt",ios::out|ios::trunc);
 2 //
 3 //    myfile<<"中國國國" << endl<< "網"<< "wgk";
 4 //    myfile.close();
 5 
 6     ifstream mfile1;
 7     mfile1.open("c:\\1.txt",ios::in);
 8     if(!mfile1){cout << "檔案錯誤";}
 9     else
10     {
11         char ch;
12         string content;
13         while(mfile1.get(ch))
14         {
15             content +=ch;
16             cout<<ch<< endl;
17         }
18         mfile1.close();
19         cout << content << endl;
20     }

C++檔案流:
fstream  // 檔案流
ifstream  // 輸入檔案流
ofstream  // 輸出檔案流

//建立一個文字檔並寫入資訊
//同向螢幕上輸出資訊一樣將資訊輸出至檔案
#include<iomanip.h>
#include<fstream.h>
void main()
{
  ofstream f1("d:\\me.txt");           //開啟檔案用於寫,若檔案不存在就建立它
  if(!f1)return;                 //開啟檔案失敗則結束運行
  f1<<setw(20)<<"姓名:"<<"廉東方"<<endl;     //使用插入運算子寫檔案內容
  f1<<setw(20)<<"家庭地址:"<<"河南鄭州"<<endl;
  f1.close();                   //關閉檔案
}
運行後開啟檔案d:\me.txt,其內容如下:
       姓名:廉東方
     家庭地址:河南鄭州

檔案操作:
開啟檔案
  檔案名稱
    注意路徑名中的斜杠要雙寫,如:
    "D:\\MyFiles\\ReadMe.txt"
  檔案開啟檔案選項:
    ios::in    = 0x01, //供讀,檔案不存在則建立(ifstream預設的開啟檔案)
    ios::out    = 0x02, //供寫,檔案不存在則建立,若檔案已存在則清空原內容(ofstream預設的開啟檔案)
    ios::ate    = 0x04, //檔案開啟時,指標在檔案最後。可改變指標的位置,常和in、out聯合使用
    ios::app    = 0x08, //供寫,檔案不存在則建立,若檔案已存在則在原檔案內容後寫入新的內容,指標位置總在最後
    ios::trunc   = 0x10, //在讀寫前先將檔案長度截斷為0(預設)
    ios::nocreate = 0x20, //檔案不存在時產生錯誤,常和in或app聯合使用
    ios::noreplace = 0x40, //檔案存在時產生錯誤,常和out聯合使用
    ios::binary  = 0x80  //二進位格式檔案
  檔案保護方式選擇項:
    filebuf::openprot;   //預設的相容共用方式
    filebuf::sh_none;    //獨佔,不共用
    filebuf::sh_read;    //讀共用
    filebuf::sh_write;   //寫共用
  開啟檔案的方法
    調用建構函式時指定檔案名稱和開啟模式
    ifstream f("d:\\12.txt",ios::nocreate);         //預設以 ios::in 的方式開啟檔案,檔案不存在時操作失敗
    ofstream f("d:\\12.txt");                //預設以 ios::out的方式開啟檔案
    fstream f("d:\\12.dat",ios::in|ios::out|ios::binary); //以讀寫方式開啟二進位檔案
    使用Open成員函數
    fstream f;
    f.open("d:\\12.txt",ios::out);             //利用同一對象對多個檔案進行操作時要用到open函數
檢查是否成功開啟
  成功:
    if(f){...}       //對ifstream、ofstream對象可用,fstream對象不可用。
    if(f.good()){...}
  失敗:
    if(!f){...}       // !運算子已經重載
    if(f.fail()){...}
讀寫操作
  使用<<,>>運算子
  只能進行文字檔的讀寫操作,用於二進位檔案可能會產生錯誤。
  使用函數成員 get、put、read、write等
  經常和read配合使用的函數是gcount(),用來獲得實際讀取的位元組數。
讀寫二進位檔案注意事項
  開啟檔案中必須指定ios::binary,否則讀寫會出錯
  用read\write進行讀寫操作,而不能使用插入、提取運算子進行操作,否則會出錯。
  使用eof()函數檢測檔案是否讀結束,使用gcount()獲得實際讀取的位元組數
關閉檔案
  使用成員函數close,如:
  f.close(); 
  利用解構函式
  對象生命期結束時會檢查檔案是否關閉,對沒有關閉的檔案進行關閉操作。
隨機讀寫檔案
  通過移動檔案讀寫指標,可在檔案指定位置進行讀寫。
  seekg(絕對位置);      //絕對移動,    //輸入資料流操作
  seekg(相對位置,參照位置);  //相對操作
  tellg();          //返回當前指標位置
  seekp(絕對位置);      //絕對移動,    //輸出資料流操作
  seekp(相對位置,參照位置);  //相對操作   
  tellp();          //返回當前指標位置
  參照位置:
  ios::beg  = 0       //相對於檔案頭
  ios::cur  = 1       //相對於當前位置
  ios::end  = 2       //相對於檔案尾
讀寫文字檔的樣本
//為能夠正確讀出寫入檔案的各資料,各資料間最好要有分隔
#include<fstream.h>
void main()
{
  fstream f("d:\\try.txt",ios::out);
  f<<1234<<' '<<3.14<<'A'<<"How are you"; //寫入資料
  f.close();
  f.open("d:\\try.txt",ios::in);
  int i;
  double d;
  char c;
  char s[20];
  f>>i>>d>>c;               //讀取資料
  f.getline(s,20);
  cout<<i<<endl;             //顯示各資料
  cout<<d<<endl;
  cout<<c<<endl;
  cout<<s<<endl;
  f.close();
}
運行結果:
1234
3.14
A
How are you
Press any key to continue
顯示文字檔的內容
//使用get()一次讀一個字元--------------------------------方案一
#include<fstream.h>
void main()
{
  ifstream fin("d:\\簡介.txt",ios::nocreate);
  if(!fin){
    cout<<"File open error!\n";
    return;
  }
  char c;
  while((c=fin.get())!=EOF)cout<<c;    //注意結束條件的判斷
  fin.close();
}
//使用get(char *,int n,char delim='\n')一次讀多個字元----方案二
//巧妙利用文字檔中不會有字元'\0'的特點進行讀取
#include<fstream.h>
void main()
{
  ifstream fin("d:\\簡介.txt",ios::nocreate);
  if(!fin){
    cout<<"File open error!\n";
    return;
  }
  char c[80];
  while(fin.get(c,80,'\0')!=NULL)cout<<c; //注意結束條件的判斷
  fin.close();
}
//使用read(char *,int n)讀檔案---------------------------方案三
#include<fstream.h>
void main()
{
  ifstream fin("d:\\簡介.txt",ios::nocreate);
  if(!fin){
    cout<<"File open error!\n";
    return;
  }
  char c[80];
  while(!fin.eof())            //判斷檔案是否讀結束
  {
    fin.read(c,80);
    cout.write(c,fin.gcount());
  }
  fin.close();
}
拷貝檔案
//二進位檔案操作樣本
#include<fstream.h>
void main()
{
  ifstream fin("C:\\1.exe",ios::nocreate|ios::binary);
  if(!fin){
    cout<<"File open error!\n";
    return;
  }
  ofstream fout("C:\\2.exe",ios::binary);
  char c[1024];
  while(!fin.eof())
  {
    fin.read(c,1024);
    fout.write(c,fin.gcount());
  }
  fin.close();
  fout.close();
  cout<<"Copy over!\n";
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.