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进行读写

猜你喜欢

转载自blog.csdn.net/dnhua/article/details/82884390