69,382
社区成员
发帖
与我相关
我的任务
分享
//打开txt文件,
FILE *F= fopen(a.txt, "w");
//一行一行读文件,循环读,读到文件尾
while( getline( &buf, &size, F) != -1 )
{
//把读到的buf内容写进bin文件
FILE *B = fopen(a.bin, "rb+");
fwrite(buf, size, 1, B);
//注意判断接口返回值
}
#include <stdio.h>
FILE *fi,*fo;
char fni[260]="i.txt";
char fno[260]="o.bin";
int v;
int main() {
fi=fopen(fni,"r");
if (NULL==fi) {
fprintf(stderr,"Can not open file %s!\n",fni);
return 1;
}
fo=fopen(fno,"wb");
if (NULL==fo) {
fclose(fi);
fprintf(stderr,"Can not create file %s!\n",fno);
return 2;
}
while (1) {
if (1!=fscanf(fi,"%d",&v)) break;
fwrite(&v,4,1,fo);//这里的4可根据实际需要改为2或1
}
fclose(fo);
fclose(fi);
return 0;
}