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"); // std::ofstream fout; fout.open("b.dat"); fout.precision(3);
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);
参考: