问题描述
在这种情况下如何使用fwrite()而不是fprintf()?我想生成
二进制文件。
FILE * fnew;
int i,intName;
double array [500];
fprintf(fnew,"%d \ n",intName);
fprintf(fnew," %f",array [i]);
我知道(我想)如何更改intName和array [i]使用
fwrite()如下:
fwrite(& intName,sizeof(int),1,fnew);
fwrite(& array [i],sizeof(double),1,fnew);
但是如何处理\ n和 "使用fwrite()?
例如,如何处理这个?
fprintf(fnew," \ n");
你为什么要这样?数据是二进制的,所以人们不会读它的价值。行格式是没有意义的。
但是,如果你坚持:
char s [] =" \ n" ;;
fwrite(s,sizeof s,1,fnew);
Josh
那么,_did_你如何()fnew?
Richard
fwrite(& intName,sizeof intName,1,fnew);
fwrite(& array [i],sizeof array [i],1,fnew);
很抱歉打扰你。
In:
fwrite(& intName,sizeof(intName),1,fnew );
fwrite(& array [i],sizeof(array [i]),1,fnew);
我明白为什么你改变了第二个。
第一个是可读性吗?或者用于维护?
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate
binary file.
FILE *fnew;
int i, intName;
double array[500];
fprintf(fnew, "%d\n", intName);
fprintf(fnew, " %f", array[i]);
I know (I suppose) how to change the "intName" and "array[i]" using
fwrite( ) as follow:
fwrite(&intName, sizeof(int), 1, fnew);
fwrite(&array[i], sizeof(double), 1, fnew);
but how to hander the "\n" and " " using fwrite( )?
for example, how to handle this?
fprintf(fnew, " \n");
Why would you want to? The data is binary, so people aren''t going to read
it. Line formatting is pointless.
But, if you insist:
char s[] = " \n";
fwrite(s, sizeof s, 1, fnew);
Josh
So, how _did_ you fopen() fnew?
Richard
fwrite(&intName, sizeof intName, 1, fnew);
fwrite(&array[i], sizeof array[i], 1, fnew);
Sorry to bother you.
In:
fwrite(&intName, sizeof (intName), 1, fnew);
fwrite(&array[i], sizeof (array[i]), 1, fnew);
I understand why you changed the second one.
Is the first more for readability? Or for maintaince?
这篇关于关于fwrite()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!