C/C++讀寫(xiě)文本文件、二進(jìn)制文件的方法
一:目的
掌握C語(yǔ)言文本文件讀寫(xiě)方式;
掌握C語(yǔ)言二進(jìn)制文件讀寫(xiě)方式;
掌握CPP文本文件讀寫(xiě)方式;
掌握CPP二進(jìn)制文件讀寫(xiě)方式;
二:C語(yǔ)言文本文件讀寫(xiě)
1. 文本文件寫(xiě)入
//采用C模式對(duì)Txt進(jìn)行寫(xiě)出
void TxtWrite_Cmode()
{
//準(zhǔn)備數(shù)據(jù)
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;
}
//寫(xiě)出txt
FILE * fid = fopen("txt_out.txt","w");
if(fid == NULL)
{
printf("寫(xiě)出文件失??!\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模式對(duì)Txt進(jìn)行讀取
void TxtRead_Cmode()
{
FILE * fid = fopen("txt_out.txt","r");
if(fid == NULL)
{
printf("打開(kāi)%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,知道數(shù)據(jù)格式,按行讀入并輸出\n");
scanf("%d",&mode);
if(mode == 1)
{
//按字符讀入并直接輸出
char ch; //讀取的字符,判斷準(zhǔn)則為ch不等于結(jié)束符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)
{
//知道數(shù)據(jù)格式,按行讀入并存儲(chǔ)輸出
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語(yǔ)言二進(jìn)制文件讀寫(xiě)
1. 二進(jìn)制文件寫(xiě)入
//采用C模式寫(xiě)二進(jìn)制文件
void DataWrite_CMode()
{
//準(zhǔn)備數(shù)據(jù)
double pos[200];
for(int i = 0; i < 200; i ++ )
pos[i] = i ;
//寫(xiě)出數(shù)據(jù)
FILE *fid;
fid = fopen("binary.dat","wb");
if(fid == NULL)
{
printf("寫(xiě)出文件出錯(cuò)");
return;
}
int mode = 1;
printf("mode為1,逐個(gè)寫(xiě)入;mode為2,逐行寫(xiě)入\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.二進(jìn)制文件讀取
//采用C模式讀二進(jìn)制文件
void DataRead_CMode()
{
FILE *fid;
fid = fopen("binary.dat","rb");
if(fid == NULL)
{
printf("讀取文件出錯(cuò)");
return;
}
int mode = 1;
printf("mode為1,知道pos有多少個(gè);mode為2,不知道pos有多少個(gè)\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);
//開(kāi)辟存儲(chǔ)空間
int num = lSize/sizeof(double);
double *pos = (double*) malloc (sizeof(double)*num);
if (pos == NULL)
{
printf("開(kāi)辟空間出錯(cuò)");
return;
}
fread(pos,sizeof(double),num,fid);
for(int i = 0; i < num; i++)
printf("%lf\n", pos[i]);
free(pos); //釋放內(nèi)存
}
fclose(fid);
}
四:C++文本文件讀寫(xiě)
1. 文本文件寫(xiě)入
//采用CPP模式寫(xiě)txt
void TxtWrite_CPPmode()
{
//準(zhǔn)備數(shù)據(jù)
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;
}
//寫(xiě)出txt
fstream f("txt_out.txt", ios::out);
if(f.bad())
{
cout << "打開(kāi)文件出錯(cuò)" << 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);
//文件打開(kāi)方式選項(xiàng):
// ios::in = 0x01, //供讀,文件不存在則創(chuàng)建(ifstream默認(rèn)的打開(kāi)方式)
// ios::out = 0x02, //供寫(xiě),文件不存在則創(chuàng)建,若文件已存在則清空原內(nèi)容(ofstream默認(rèn)的打開(kāi)方式)
// ios::ate = 0x04, //文件打開(kāi)時(shí),指針在文件最后??筛淖冎羔樀奈恢?,常和in、out聯(lián)合使用
// ios::app = 0x08, //供寫(xiě),文件不存在則創(chuàng)建,若文件已存在則在原文件內(nèi)容后寫(xiě)入新的內(nèi)容,指針位置總在最后
// ios::trunc = 0x10, //在讀寫(xiě)前先將文件長(zhǎng)度截?cái)酁?(默認(rèn))
// ios::nocreate = 0x20, //文件不存在時(shí)產(chǎn)生錯(cuò)誤,常和in或app聯(lián)合使用
// ios::noreplace = 0x40, //文件存在時(shí)產(chǎn)生錯(cuò)誤,常和out聯(lián)合使用
// ios::binary = 0x80 //二進(jìn)制格式文件
vector<int> index;
vector<double> x_pos;
vector<double> y_pos;
if(!f)
{
cout << "打開(kāi)文件出錯(cuò)" << endl;
return;
}
cout<<"mode為1,按字符讀入并輸出;mode為2,按行讀入輸出;mode為3,知道數(shù)據(jù)格式,按行讀入并輸出"<<endl;
int mode = 1;
cin>>mode;
if(1== mode)
{
//按字節(jié)讀入并輸出
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); //跳過(guò)字節(jié)
//seekg(絕對(duì)位置); //絕對(duì)移動(dòng), //輸入流操作
//seekg(相對(duì)位置,參照位置); //相對(duì)操作
//tellg(); //返回當(dāng)前指針位置
while(!f.eof())
{
//使用eof()函數(shù)檢測(cè)文件是否讀結(jié)束
f.getline(line,128);
numBytes = f.gcount(); //使用gcount()獲得實(shí)際讀取的字節(jié)數(shù)
cout << line << "\t" << numBytes << "字節(jié)" << endl ;
}
}
else if(3 == mode)
{
//如果知道數(shù)據(jù)格式,可以直接用>>讀入
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++二進(jìn)制文件讀寫(xiě)
1. 二進(jìn)制文件寫(xiě)入
//采用CPP模式寫(xiě)二進(jìn)制文件
void DataWrite_CPPMode()
{
//準(zhǔn)備數(shù)據(jù)
double pos[200];
for(int i = 0; i < 200; i ++ )
pos[i] = i ;
//寫(xiě)出數(shù)據(jù)
ofstream f("binary.dat",ios::binary);
if(!f)
{
cout << "創(chuàng)建文件失敗" <<endl;
return;
}
f.write((char*)pos, 200*sizeof(double)); //fwrite以char *的方式進(jìn)行寫(xiě)出,做一個(gè)轉(zhuǎn)化
f.close();
}
2.二進(jìn)制文件讀取
//采用CPP模式讀二進(jìn)制文件
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();
}
六 總結(jié)
1. C語(yǔ)言讀寫(xiě)文件均通過(guò)FILE指針執(zhí)行操作,其中文本文件的讀寫(xiě)用fprintf,fscanf,二進(jìn)制文件的讀寫(xiě)用fread,fwrite
2. C++讀寫(xiě)文件通過(guò)fstream、ifstream、ofstream進(jìn)行操作,文本文件用<< 和 >> 進(jìn)行讀寫(xiě),二進(jìn)制文件用read和write進(jìn)行讀寫(xiě)
以上這篇C/C++讀寫(xiě)文本文件、二進(jìn)制文件的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- C++使用read()和write()讀寫(xiě)二進(jìn)制文件
- C++?二進(jìn)制文件讀寫(xiě)方式及示例詳解
- C\C++實(shí)現(xiàn)讀寫(xiě)二進(jìn)制文件的方法詳解
- C++用read()和write()讀寫(xiě)二進(jìn)制文件的超詳細(xì)教程
- C++讀寫(xiě)(CSV,Yaml,二進(jìn)制)文件的方法詳解
- C/C++中二進(jìn)制文件&順序讀寫(xiě)詳解及其作用介紹
- 利用C/C++二進(jìn)制讀寫(xiě)png文件的方法示例
- 詳解C++編程中對(duì)二進(jìn)制文件的讀寫(xiě)操作
- C++實(shí)現(xiàn)文本與二進(jìn)制文件讀寫(xiě)操作的示例
相關(guān)文章
QT基于TCP實(shí)現(xiàn)文件傳輸系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了QT基于TCP實(shí)現(xiàn)文件傳輸系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
C++各種數(shù)據(jù)類(lèi)型所占內(nèi)存大小詳解
這篇文章主要介紹了C++各種數(shù)據(jù)類(lèi)型所占內(nèi)存大小,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
深入學(xué)習(xí)C語(yǔ)言中的函數(shù)指針和左右法則
這篇文章主要介紹了深入學(xué)習(xí)C語(yǔ)言中的函數(shù)指針和左右法則,左右法則是一種常用的C指針聲明,需要的朋友可以參考下2015-08-08
C++用boost.signal實(shí)現(xiàn)多播委托
這篇文章介紹了C++用boost.signal實(shí)現(xiàn)多播委托的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
C++?vector的常見(jiàn)用法超詳細(xì)講解
這篇文章主要介紹了C++?vector的常見(jiàn)用法,包括C++中vector容器的定義、初始化方法、訪問(wèn)元素、常用函數(shù)及其時(shí)間復(fù)雜度,通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04

