(C/C++)(讀/寫)(二進位制檔案/文字檔案)
阿新 • • 發佈:2018-11-08
C++寫二進位制檔案
std::ofstream fout("a.dat", std::ios::binary);
int nNum = 20;
std::string str("Hello, world");
fout.write((char*)&nNum, sizeof(int));
fout.write(str.c_str(), sizeof(char)*(str.size()));
fout.close();
C++讀二進位制檔案
std::ifstream fin("a.dat", std::ios::binary); int nNum; char szBuf[256] = {0}; fin.read((char*)&nNum, sizeof(int)); fin.read(szBuf, sizeof(char) * 256); std::cout << "int = " << nNum << std::endl; std::cout << "str = " << szBuf << std::endl; fin.close();
C++寫文字檔案
std::ofstream fout("b.dat");
int nNum = 20;
std::string str("Hello, world");
fout << nNum << " " << str << std::endl;
fout.close();
C++讀文字檔案
std::ifstream fin("b.dat"); int nNum; char szBuf[256] = {0}; fin >> nNum >> szBuf; std::cout << "int = " << nNum << std::endl; std::cout << "str = " << szBuf << std::endl; fin.close();
檔案的開啟模式
檔案操作時,如果不顯示指定開啟模式,檔案流類將使用預設值。
在<fstream> 中定義瞭如下開啟模式和檔案屬性:
ios::app // 從後面新增
ios::ate // 開啟並找到檔案尾
ios::binary // 二進位制模式I/O(與文字模式相對)
ios::in // 只讀開啟
ios::out // 寫開啟
ios::trunc // 將檔案截為 0 長度
可以使用位操作符 OR 組合這些標誌,比如
ofstream logFile("log.dat", ios::binary | ios::app);
--------------------------------------------
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);
}
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寫文字檔案
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);
}
C讀文字檔案
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讀取二進位制影象資料
FILE *fp;
string path="../img"+to_string(index)+".png";
fp=fopen(path.c_str(),"rb");
if(!fp)
{
cout<<"error!"<<endl;
return -1;
}
int head;
fread(&head,sizeof(head),1,fp);
uint8_t *img_addr=new uint8_t[480*640];
fread(img_addr,480*640,1,fp);
Mat img=Mat(480,640,CV_8U,img_addr);
fclose(fp);
參考: