且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

关于fwrite()

更新时间:2023-12-04 12:57:52

2003年10月13日星期一22:12:50 +0800,FrancisC写道:
如何使用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);


fwrite(& intName,sizeof intName,1,fnew);

fwrite(& array [i],sizeof array [i],1 ,fnew);

但是如何处理\ n和 "使用fwrite()?
例如,如何处理这个?

fprintf(fnew," \ n");




你为什么要这样?数据是二进制的,所以人们不会读它的价值。行格式是没有意义的。


但是,如果你坚持:


char s [] =" \ n&quot ;;

fwrite(s,sizeof s,1,fnew);


Josh


" FrancisC" < FR ********** @ hong-kong.crosswinds.net>写道:
在这种情况下如何使用fwrite()而不是fprintf()?我想生成
二进制文件。


生成二进制文件与是否使用fwrite()

或fprintf()无关,以及是否fopen()输出流

为w (写 - 文本模式)或wb; (写 - 二进制模式)。

FILE * fnew;
int i,intName;
双数组[500];

fprintf (fnew,"%d \ n",intName);




那么,_did_你如何()fnew?


Richard


Josh Sebastian写道:

FILE * fnew ;
int i,intName;
double array [500];
fwrite(& intName,sizeof(int),1,fnew);
fwrite(& array [i],sizeof(double),1,fnew);



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");

On Mon, 13 Oct 2003 22:12:50 +0800, FrancisC wrote:
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);
fwrite(&intName, sizeof intName, 1, fnew);
fwrite(&array[i], sizeof array[i], 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


"FrancisC" <fr**********@hong-kong.crosswinds.net> wrote:
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate
binary file.
Generating binary files has nothing to do with whether you use fwrite()
or fprintf(), and everything with whether you fopen() your output stream
as "w" ("write - text mode") or "wb" ("write - binary mode").
FILE *fnew;
int i, intName;
double array[500];

fprintf(fnew, "%d\n", intName);



So, how _did_ you fopen() fnew?

Richard


Josh Sebastian wrote:

FILE *fnew;
int i, intName;
double array[500]; fwrite(&intName, sizeof(int), 1, fnew);
fwrite(&array[i], sizeof(double), 1, fnew);


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?