编写程序从文本文件in.txt读入10个整数,以二进制方式写入一个名为out.bin的新文件中。这样写对么? 10
#include<stdio.h>#include<stdlib.h>main(){inti,num[10];FILE*fr,*fw;if((fr=fopen("in.t...
#include <stdio.h>
#include <stdlib.h>
main()
{
int i,num[10];
FILE *fr,*fw;
if((fr=fopen ( "in.txt","r"))==NULL)
{
printf("can't open file: in.txt\n");
exit(0);
}
if((fw=fopen ( "out.bin","wb"))==NULL)
{
printf("can't open file: res.dat\n");
exit(0);
}
for(i=0;i<10;i++)
{
fscanf(fr,"%d",&num[i]);
}
for(i=0;i<10;i++)
fwrite(&num[i],sizeof(int),1,fw);
fclose(fr);
fclose(fw);
}读文本用fscanf,二进制些用fwrite,如果文本方式用fprintf么? 展开
#include <stdlib.h>
main()
{
int i,num[10];
FILE *fr,*fw;
if((fr=fopen ( "in.txt","r"))==NULL)
{
printf("can't open file: in.txt\n");
exit(0);
}
if((fw=fopen ( "out.bin","wb"))==NULL)
{
printf("can't open file: res.dat\n");
exit(0);
}
for(i=0;i<10;i++)
{
fscanf(fr,"%d",&num[i]);
}
for(i=0;i<10;i++)
fwrite(&num[i],sizeof(int),1,fw);
fclose(fr);
fclose(fw);
}读文本用fscanf,二进制些用fwrite,如果文本方式用fprintf么? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询