C/C++讀寫文本文件、二進制文件的方法
一:目的
掌握C語言文本文件讀寫方式;
掌握C語言二進制文件讀寫方式;
掌握CPP文本文件讀寫方式;
掌握CPP二進制文件讀寫方式;
二:C語言文本文件讀寫
1. 文本文件寫入
//采用C模式對Txt進行寫出
void TxtWrite_Cmode()
{
//準備數(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;
}
//寫出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,知道數(shù)據(jù)格式,按行讀入并輸出\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)
{
//知道數(shù)據(jù)格式,按行讀入并存儲輸出
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()
{
//準備數(shù)據(jù)
double pos[200];
for(int i = 0; i < 200; i ++ )
pos[i] = i ;
//寫出數(shù)據(jù)
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()
{
//準備數(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;
}
//寫出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, //供讀,文件不存在則創(chuàng)建(ifstream默認的打開方式)
// ios::out = 0x02, //供寫,文件不存在則創(chuàng)建,若文件已存在則清空原內容(ofstream默認的打開方式)
// ios::ate = 0x04, //文件打開時,指針在文件最后??筛淖冎羔樀奈恢茫:蚷n、out聯(lián)合使用
// ios::app = 0x08, //供寫,文件不存在則創(chuàng)建,若文件已存在則在原文件內容后寫入新的內容,指針位置總在最后
// ios::trunc = 0x10, //在讀寫前先將文件長度截斷為0(默認)
// ios::nocreate = 0x20, //文件不存在時產(chǎn)生錯誤,常和in或app聯(lián)合使用
// ios::noreplace = 0x40, //文件存在時產(chǎn)生錯誤,常和out聯(lián)合使用
// 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,知道數(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); //跳過字節(jié)
//seekg(絕對位置); //絕對移動, //輸入流操作
//seekg(相對位置,參照位置); //相對操作
//tellg(); //返回當前指針位置
while(!f.eof())
{
//使用eof()函數(shù)檢測文件是否讀結束
f.getline(line,128);
numBytes = f.gcount(); //使用gcount()獲得實際讀取的字節(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++二進制文件讀寫
1. 二進制文件寫入
//采用CPP模式寫二進制文件
void DataWrite_CPPMode()
{
//準備數(shù)據(jù)
double pos[200];
for(int i = 0; i < 200; i ++ )
pos[i] = i ;
//寫出數(shù)據(jù)
ofstream f("binary.dat",ios::binary);
if(!f)
{
cout << "創(chuàng)建文件失敗" <<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指針執(zhí)行操作,其中文本文件的讀寫用fprintf,fscanf,二進制文件的讀寫用fread,fwrite
2. C++讀寫文件通過fstream、ifstream、ofstream進行操作,文本文件用<< 和 >> 進行讀寫,二進制文件用read和write進行讀寫
以上這篇C/C++讀寫文本文件、二進制文件的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持我們。
上一篇:用C/C++實現(xiàn)linux下檢測網(wǎng)絡接口狀態(tài)
欄 目:C語言
下一篇:解決C++ fopen按行讀取文件及所讀取的數(shù)據(jù)問題
本文標題:C/C++讀寫文本文件、二進制文件的方法
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/744.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關鍵字的使用詳解
- 01-10深入C/C++浮點數(shù)在內存中的存儲方式詳解
- 01-10深入理解C/C++混合編程


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-10C#中split用法實例總結
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子


