C/C++读写文本文件、二进制文件的方法

一:目的

掌握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;}//写出txtFILE * 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模式写txtvoid 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;}//写出txtfstream 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模式读取txtvoid 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进行读写

以上这篇C/C++读写文本文件、二进制文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

世上没有绝望的处境,只有对处境绝望的人。

C/C++读写文本文件、二进制文件的方法

相关文章:

你感兴趣的文章:

标签云: