//c的输入输出二进制文件
#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<fstream>
#include<string>
using namespace std;
struct test
{
int i;
char j[10];
};
int main()
{
test test1={1,"test"},test2;
ofstream pf("a1.txt",ios_base::binary);
pf.write((const char *)&test1,sizeof(test));
pf.close();
ifstream pf1("a1.txt",ios_base::binary);
pf1.read((echar *)&test2,sizeof(test));
printf("%d %s\n",test2.i,test2.j);
pf1.close();
return 0;
}
//c++的输入输出二进制文件
#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<fstream>
#include<string>
using namespace std;
struct test
{
int i;
char j[10];
};
int main()
{
test test1={1,"test"},test2;
FILE *pf;
pf=fopen("a1.txt","wb");
fwrite(&test1,sizeof(test),1,pf);
fclose(pf);
pf=fopen("a1.txt","rb");
fread(&test2,sizeof(test),1,pf);
fclose(pf);
printf("%d %s\n",test2.i,test2.j);
return 0;
}
#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<fstream>
#include<string>
using namespace std;
struct test
{
int i;
char j[10];
};
int main()
{
test test1={1,"test"},test2;
ofstream pf("a1.txt",ios_base::binary);
pf.write((const char *)&test1,sizeof(test));
pf.close();
ifstream pf1("a1.txt",ios_base::binary);
pf1.read((echar *)&test2,sizeof(test));
printf("%d %s\n",test2.i,test2.j);
pf1.close();
return 0;
}
//c++的输入输出二进制文件
#include"aa.h"
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<fstream>
#include<string>
using namespace std;
struct test
{
int i;
char j[10];
};
int main()
{
test test1={1,"test"},test2;
FILE *pf;
pf=fopen("a1.txt","wb");
fwrite(&test1,sizeof(test),1,pf);
fclose(pf);
pf=fopen("a1.txt","rb");
fread(&test2,sizeof(test),1,pf);
fclose(pf);
printf("%d %s\n",test2.i,test2.j);
return 0;
}
本文介绍使用C++进行二进制文件的读写操作。通过示例代码展示了如何利用C++标准库中的fstream及C语言的文件操作函数fwrite和fread来完成结构体数据的写入与读取过程。
1165

被折叠的 条评论
为什么被折叠?



