我想将计算后的数据写入一个 .txt文件中
FILE *xzf;
int a[10];
a[1]=1;a[2]=1;a[3]=1;a[4]=1;
if ((xzf=fopen("xzf.txt","wb"))==NULL)
{ printf("cannot open the file!\n");
exit(0);
}
fwrite(a,sizeof(int),4,xzf);
fclose(xzf);
printf("\n data write in xzf.dat\n");
为什么结果成了这冬冬: 烫烫
FILE *xzf;
int a[10];
a[1]=1;a[2]=1;a[3]=1;a[4]=1;
if ((xzf=fopen("xzf.txt","wb"))==NULL)
{ printf("cannot open the file!\n");
exit(0);
}
fwrite(a,sizeof(int),4,xzf);
fclose(xzf);
printf("\n data write in xzf.dat\n");
为什么结果成了这冬冬: 烫烫
如果你要写入的数据要能看懂, 转成字符串以后再写吧。
000000 00 00 00 00 01 00 00 00 02 00 00 00 03 00 00 00...............
000010
4跑哪去了?有没有办法变成这种形式: 0.00 1.00 2.00 3.00 4.00
多指教!谢谢
先显示成你说的方式 FILE *xzf;
int a[10];
a[0]=0;a[1]=1;a[2]=1;a[3]=1;a[4]=1;
if ((xzf=fopen("xzf.txt","wb"))==NULL)
{
printf("cannot open the file!\n");
exit(0);
} for(int i = 0; i < 5; i++)
{
fprintf(xzf, "%.2lf ", a[i];
}
fclose(xzf);
printf("\n data write in xzf.dat\n");
FILE *xzf;
FILE *gyz;
int i;
double a[10];
double b[10];
a[0]=0.00;a[1]=1.00;a[2]=2.00;a[3]=3.00;a[4]=4.00;
case 1:
if ((xzf=fopen("xzf.txt","wb"))==NULL)
{ printf("cannot open the file!\n");
exit(0);
}
// fwrite(a,sizeof(double),5,xzf);
for(i = 0; i < 5; i++)
{ 这里只能用fprintf,不能用fwrite
fprintf(xzf, "%f", a[i]);
}
fclose(xzf);
printf("\n data write in xzf.dat\n");
break;
case 2:
if((xzf=fopen("xzf.txt","rb"))==NULL)
{
printf("cannot open the file\n");
exit(0);
}
fread(b,sizeof(double),5,xzf);
fclose(xzf); if ((gyz=fopen("gyz.txt","wb"))==NULL)
{ printf("cannot open the file!\n");
exit(0);
}
fwrite(b,sizeof(double),5,gyz);
// for(i = 0; i < 5; i++)
// { 这里只能用fprintf,不能用fwrite
// fprintf(gyz, "%f", b[i]);
// }
fclose(gyz);
为什么两次写文件用的两个命令不一样,否则出现乱码
fclose(xzf); if ((gyz=fopen("gyz.txt","wb"))==NULL)
{ printf("cannot open the file!\n");
exit(0);
}
fwrite(b,sizeof(double),5,gyz); 去看看c语言的书吧