本文介绍了关于fwrite()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下如何使用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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 21:49