1. 程式人生 > >C++檔案讀取

C++檔案讀取

c++檔案和流:ttp://www.runoob.com/cplusplus/cpp-files-streams.html

到目前為止,我們已經使用了 iostream 標準庫,它提供了 cincout 方法分別用於從標準輸入讀取流和向標準輸出寫入流。

本教程介紹如何從檔案讀取流和向檔案寫入流。這就需要用到 C++ 中另一個標準庫 fstream,它定義了三個新的資料型別:

資料型別 描述
ofstream 該資料型別表示輸出檔案流,用於建立檔案並向檔案寫入資訊。
ifstream 該資料型別表示輸入檔案流,用於從檔案讀取資訊。
fstream 該資料型別通常表示檔案流,且同時具有 ofstream 和 ifstream 兩種功能,這意味著它可以建立檔案,向檔案寫入資訊,從檔案讀取資訊。

要在 C++ 中進行檔案處理,必須在 C++ 原始碼檔案中包含標頭檔案 <iostream> 和 <fstream>。

開啟檔案

在從檔案讀取資訊或者向檔案寫入資訊之前,必須先開啟檔案。ofstreamfstream 物件都可以用來開啟檔案進行寫操作,如果只需要開啟檔案進行讀操作,則使用 ifstream 物件。

下面是 open() 函式的標準語法,open() 函式是 fstream、ifstream 和 ofstream 物件的一個成員。

void open(const char *filename, ios::openmode mode);

在這裡,open() 成員函式的第一引數指定要開啟的檔案的名稱和位置,第二個引數定義檔案被開啟的模式。

模式標誌 描述
ios::app 追加模式。所有寫入都追加到檔案末尾。
ios::ate 檔案開啟後定位到檔案末尾。
ios::in 開啟檔案用於讀取。
ios::out 開啟檔案用於寫入。
ios::trunc 如果該檔案已經存在,其內容將在開啟檔案之前被截斷,即把檔案長度設為 0。

您可以把以上兩種或兩種以上的模式結合使用。例如,如果您想要以寫入模式開啟檔案,並希望截斷檔案,以防檔案已存在,那麼您可以使用下面的語法:

ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );

類似地,您如果想要開啟一個檔案用於讀寫,可以使用下面的語法:

fstream  afile;
afile.open("file.dat", ios::out | ios::in );

關閉檔案

當 C++ 程式終止時,它會自動關閉重新整理所有流,釋放所有分配的記憶體,並關閉所有開啟的檔案。但程式設計師應該養成一個好習慣,在程式終止前關閉所有開啟的檔案。

下面是 close() 函式的標準語法,close() 函式是 fstream、ifstream 和 ofstream 物件的一個成員。

void close();

寫入檔案

在 C++ 程式設計中,我們使用流插入運算子( << )向檔案寫入資訊,就像使用該運算子輸出資訊到螢幕上一樣。唯一不同的是,在這裡您使用的是 ofstreamfstream 物件,而不是 cout 物件。

讀取檔案

在 C++ 程式設計中,我們使用流提取運算子( >> )從檔案讀取資訊,就像使用該運算子從鍵盤輸入資訊一樣。唯一不同的是,在這裡您使用的是 ifstreamfstream 物件,而不是 cin 物件。

讀取 & 寫入例項

下面的 C++ 程式以讀寫模式開啟一個檔案。在向檔案 afile.dat 寫入使用者輸入的資訊之後,程式從檔案讀取資訊,並將其輸出到螢幕上:

例項

#include <fstream> #include <iostream> using namespace std; int main () { char data[100]; // 以寫模式開啟檔案 ofstream outfile; outfile.open("afile.dat"); cout << "Writing to the file" << endl; cout << "Enter your name: "; cin.getline(data, 100); // 向檔案寫入使用者輸入的資料 outfile << data << endl; cout << "Enter your age: "; cin >> data; cin.ignore(); // 再次向檔案寫入使用者輸入的資料 outfile << data << endl; // 關閉開啟的檔案 outfile.close(); // 以讀模式開啟檔案 ifstream infile; infile.open("afile.dat"); cout << "Reading from the file" << endl; infile >> data; // 在螢幕上寫入資料 cout << data << endl; // 再次從檔案讀取資料,並顯示它 infile >> data; cout << data << endl; // 關閉開啟的檔案 infile.close(); return 0; }

當上面的程式碼被編譯和執行時,它會產生下列輸入和輸出:

$./a.out
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9

上面的例項中使用了 cin 物件的附加函式,比如 getline()函式從外部讀取一行,ignore() 函式會忽略掉之前讀語句留下的多餘字元。

檔案位置指標

istreamostream 都提供了用於重新定位檔案位置指標的成員函式。這些成員函式包括關於 istream 的 seekg("seek get")和關於 ostream 的 seekp("seek put")。

seekg 和 seekp 的引數通常是一個長整型。第二個引數可以用於指定查詢方向。查詢方向可以是 ios::beg(預設的,從流的開頭開始定位),也可以是 ios::cur(從流的當前位置開始定位),也可以是 ios::end(從流的末尾開始定位)。

檔案位置指標是一個整數值,指定了從檔案的起始位置到指標所在位置的位元組數。下面是關於定位 "get" 檔案位置指標的例項:

// 定位到 fileObject 的第 n 個位元組(假設是 ios::beg) fileObject.seekg( n ); // 把檔案的讀指標從 fileObject 當前位置向後移 n 個位元組 fileObject.seekg( n, ios::cur ); // 把檔案的讀指標從 fileObject 末尾往回移 n 個位元組 fileObject.seekg( n, ios::end ); // 定位到 fileObject 的末尾 fileObject.seekg( 0, ios::end );

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------

c++如何讀取二進位制流檔案:https://blog.csdn.net/lightblueme/article/details/24181173

數字影象處理的作業中要讀取一個二進位制檔案。所有的灰度值為0-255. 使用下面的方法讀取時出現了問題。

 

ifstream fin;
 fin.open("data_batch_1.bin",ios::binary);
 if(!fin){
  cout<<"open error!"<<endl;
  return -1;
 }
 char buffer[3073];
 fin.read(buffer,3073*sizeof(char));
 for(int i=0;i<17;i++){
  cout<<(unsigned short)buffer[i]<<endl;
 }
由於char是有符號的,因此在使用unsigned short進行轉化的時候,也會出現問題。可以改為下面的程式碼。

 


ifstream fin;
 fin.open("data_batch_1.bin",ios::binary);
 if(!fin){
  cout<<"open error!"<<endl;
  return -1;
 }
 char buffer[3073];
 fin.read(buffer,3073*sizeof(char));
 for(int i=0;i<17;i++){
  unsigned char tmp=(unsigned char)buffer[i];
  cout<<(unsigned short)tmp<<endl;
 }

/*------------------------------------------------------------------------------------------------------------------------------------------------------------------------

C/C++讀寫文字檔案、二進位制檔案:https://blog.csdn.net/nichengwuxiao/article/details/78789225

一:目的

 

掌握C語言文字檔案讀寫方式;

 

 

 

掌握C語言二進位制檔案讀寫方式;

 

 

 

掌握CPP文字檔案讀寫方式;

 

 

 

掌握CPP二進位制檔案讀寫方式;

 

二:C語言文字檔案讀寫

 


1. 文字檔案寫入
//採用C模式對Txt進行寫出
void TxtWrite_Cmode()
{
 //準備資料
 int index[50] ;
 double x_pos[50], y_pos[50];
 for(int i = 0; i < 50; i ++ )
 {
  index[i] = i;
  x_pos[i] = rand()%1000 * 0.01 ;
  y_pos[i] = rand()%2000 * 0.01;
 }
 //寫出txt
 FILE * fid = fopen("txt_out.txt","w");
 if(fid == NULL)
 {
  printf("寫出檔案失敗!\n");
  return;
 }
 for(int i = 0; i < 50; i ++ )
 {
  fprintf(fid,"%03d\t%4.6lf\t%4.6lf\n",index[i],x_pos[i],y_pos[i]);
 }
 fclose(fid);
}2. 文字檔案讀取
//採用C模式對Txt進行讀取
void TxtRead_Cmode()
{
 FILE * fid = fopen("txt_out.txt","r");
 if(fid == NULL)
 {
  printf("開啟%s失敗","txt_out.txt");
  return;
 }
 vector<int> index;
 vector<double> x_pos;
 vector<double> y_pos;
 int mode = 1;
 printf("mode為1,按字元讀入並輸出;mode為2,按行讀入輸出;mode為3,知道資料格式,按行讀入並輸出\n");
 scanf("%d",&mode);
 if(mode == 1)
 {
  //按字元讀入並直接輸出
  char ch;       //讀取的字元,判斷準則為ch不等於結束符EOF(end of file)
  while(EOF!=(ch= fgetc(fid)))
    printf("%c", ch);
 }
 else if(mode == 2)
 {
  char line[1024];
  memset(line,0,1024);
  while(!feof(fid))
  {
   fgets(line,1024,fid);
   printf("%s\n", line); //輸出
  }
 }
 else if(mode == 3)
 {
  //知道資料格式,按行讀入並存儲輸出
  int index_tmp;
  double x_tmp, y_tmp;
  while(!feof(fid)) 
  { 
   fscanf(fid,"%d%lf%lf\n",&index_tmp, &x_tmp, &y_tmp);
   index.push_back(index_tmp);
   x_pos.push_back(x_tmp);
   y_pos.push_back(y_tmp);
  }
  for(int i = 0; i < index.size(); i++)
   printf("%04d\t%4.8lf\t%4.8lf\n",index[i], x_pos[i], y_pos[i]);

 

 }
 fclose(fid);
}

 


三:C語言二進位制檔案讀寫

 

 

 

1. 二進位制檔案寫入
//採用C模式寫二進位制檔案
void DataWrite_CMode()
{
 //準備資料
 double pos[200];
 for(int i = 0; i < 200; i ++ )
  pos[i] = i ;
 //寫出資料
 FILE *fid;
 fid = fopen("binary.dat","wb");
 if(fid == NULL)
 {
  printf("寫出檔案出錯");
  return;
 }
 int mode = 1;
 printf("mode為1,逐個寫入;mode為2,逐行寫入\n");
 scanf("%d",&mode);
 if(1==mode)
 {
  for(int i = 0; i < 200; i++)
   fwrite(&pos[i],sizeof(double),1,fid);
 }
 else if(2 == mode)
 {
  fwrite(pos, sizeof(double), 200, fid);
 }
 fclose(fid);
}2.二進位制檔案讀取

 

 

 

 

 

//採用C模式讀二進位制檔案
void DataRead_CMode()
{
 FILE *fid;
 fid = fopen("binary.dat","rb");
 if(fid == NULL)
 {
  printf("讀取檔案出錯");
  return;
 }
 int mode = 1;
 printf("mode為1,知道pos有多少個;mode為2,不知道pos有多少個\n");
 scanf("%d",&mode);
 if(1 == mode)
 {
  double pos[200];
  fread(pos,sizeof(double),200,fid);
  for(int i = 0; i < 200; i++)
   printf("%lf\n", pos[i]);
  free(pos);
 }
 else if(2 == mode)
 {
  //獲取檔案大小
  fseek (fid , 0 , SEEK_END);      
  long lSize = ftell (fid); 
  rewind (fid);
  //開闢儲存空間
  int num = lSize/sizeof(double);
  double *pos = (double*) malloc (sizeof(double)*num); 
  if (pos == NULL) 
  { 
   printf("開闢空間出錯");  
   return;
  }
  fread(pos,sizeof(double),num,fid);
  for(int i = 0; i < num; i++)
   printf("%lf\n", pos[i]);
  free(pos);     //釋放記憶體
 }
 fclose(fid);
}

 

 

 

 

 

四:C++文字檔案讀寫

 

 

 

 

 

1. 文字檔案寫入
//採用CPP模式寫txt
void TxtWrite_CPPmode()
{
 //準備資料
 int index[50] ;
 double x_pos[50], y_pos[50];
 for(int i = 0; i < 50; i ++ )
 {
  index[i] = i;
  x_pos[i] = rand()%1000 * 0.01 ;
  y_pos[i] = rand()%2000 * 0.01;
 }
 //寫出txt
 fstream f("txt_out.txt", ios::out);
 if(f.bad())
 {
  cout << "開啟檔案出錯" << endl;
  return;
 }
 for(int i = 0; i < 50; i++)
  f << setw(5) << index[i] << "\t" << setw(10) << x_pos[i] <<"\t" <<setw(10)<< y_pos[i] << endl;
 f.close();

 

}2.文字檔案讀取
//採用CPP模式讀取txt
void TextRead_CPPmode()
{
 fstream f;
 f.open("txt_out.txt",ios::in); 
 //檔案開啟方式選項:
 // 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  //二進位制格式檔案
 vector<int> index;
 vector<double> x_pos;
 vector<double> y_pos;
 if(!f)
 {
  cout << "開啟檔案出錯" << endl;
  return;
 }
 cout<<"mode為1,按字元讀入並輸出;mode為2,按行讀入輸出;mode為3,知道資料格式,按行讀入並輸出"<<endl;
 int mode = 1;
 cin>>mode;
 if(1== mode)
 {
  //按位元組讀入並輸出
  char ch;
  while(EOF != (ch= f.get()))
   cout << ch;
 }
 else if(2 == mode)
 {
  //按行讀取,並顯示
  char line[128];
  int numBytes;
  f.getline(line,128);
  cout << line << "\t" << endl ;
  f.getline(line,128);
  cout << line << "\t" << endl ;
  f.seekg(0,0);       //跳過位元組
  //seekg(絕對位置);      //絕對移動,    //輸入流操作
  //seekg(相對位置,參照位置);  //相對操作
  //tellg();      //返回當前指標位置
  while(!f.eof())
  {
   //使用eof()函式檢測檔案是否讀結束
   f.getline(line,128);
   numBytes = f.gcount();  //使用gcount()獲得實際讀取的位元組數
   cout << line << "\t" << numBytes << "位元組" << endl ;
  }
 }
 else if(3 == mode)
 {
  //如果知道資料格式,可以直接用>>讀入
  int index_temp = 0;
  double x_pos_temp = 0, y_pos_temp = 0;
  while(!f.eof())
  {
   f >> index_temp >> x_pos_temp >> y_pos_temp ;
   index.push_back(index_temp);
   x_pos.push_back(x_pos_temp);
   y_pos.push_back(y_pos_temp);
   cout << index_temp << "\t" << x_pos_temp << "\t" << y_pos_temp << endl;
  }
 }
 f.close();
}

 

 

 

 

 


五:C++二進位制檔案讀寫

 

 

 

 

 


1. 二進位制檔案寫入
//採用CPP模式寫二進位制檔案
void DataWrite_CPPMode()
{
 //準備資料
 double pos[200];
 for(int i = 0; i < 200; i ++ )
  pos[i] = i ;
 //寫出資料
 ofstream f("binary.dat",ios::binary);
 if(!f)
 {
  cout << "建立檔案失敗" <<endl;
  return;
 }
 f.write((char*)pos, 200*sizeof(double));      //fwrite以char *的方式進行寫出,做一個轉化
 f.close();
}2.二進位制檔案讀取
//採用CPP模式讀二進位制檔案
void DataRead_CPPMode()
{
 double pos[200];
 ifstream f("binary.dat", ios::binary);
 if(!f)
 {
  cout << "讀取檔案失敗" <<endl;
  return;
 }
 f.read((char*)pos,200*sizeof(double));
 for(int i = 0; i < 200; i++)
  cout << pos[i] <<endl;
 f.close();

 

}

 

 

 


六 總結
1. C語言讀寫檔案均通過FILE指標執行操作,其中文字檔案的讀寫用fprintf,fscanf,二進位制檔案的讀寫用fread,fwrite
2. C++讀寫檔案通過fstream、ifstream、ofstream進行操作,文字檔案用<< 和 >> 進行讀寫,二進位制檔案用read和write進行讀寫