about fwrite( )

F

FrancisC

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 know (I suppose) how to change the "intName" and "array" using
fwrite( ) as follow:

fwrite(&intName, sizeof(int), 1, fnew);
fwrite(&array, sizeof(double), 1, fnew);

but how to hander the "\n" and " " using fwrite( )?
for example, how to handle this?

fprintf(fnew, " \n");
 
J

Josh Sebastian

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 know (I suppose) how to change the "intName" and "array" using
fwrite( ) as follow:

fwrite(&intName, sizeof(int), 1, fnew);
fwrite(&array, sizeof(double), 1, fnew);


fwrite(&intName, sizeof intName, 1, fnew);
fwrite(&array, sizeof array, 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
 
R

Richard Bos

FrancisC said:
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
 
F

FrancisC

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 know (I suppose) how to change the "intName" and "array" using
fwrite( ) as follow:

fwrite(&intName, sizeof(int), 1, fnew);
fwrite(&array, sizeof(double), 1, fnew);


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


why use sizeof (intName) and sizeof (array) instead of sizeof(int) and
sizeof(double) ?
intName and array[] is set as int and double respectively
so are they both correct?
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);

I want to maintain a file structure for later reading from the binary file.
So I think i need to, dont I?
 
F

FrancisC

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

I use fnew = fopen(newFileName, "wb"), but I still can see the content
using notepad or other text editor, so i suppose it is ASCII, but I want to
generate binary file.
 
S

SomeDumbGuy

Josh Sebastian wrote:

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



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


Sorry to bother you.
In:

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

I understand why you changed the second one.
Is the first more for readability? Or for maintaince?
 
J

Josh Sebastian

fwrite(&intName, sizeof(int), 1, fnew);
fwrite(&array, sizeof(double), 1, fnew);


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


why use sizeof (intName) and sizeof (array) instead of sizeof(int) and
sizeof(double) ?
intName and array[] is set as int and double respectively
so are they both correct?


Yes, they are both correct, but what if you want to later change the type
of intName from, say, an int to a long? If you do it your way, you have to
search through the source and possibly change it where you shouldn't have.
With my way, you change it once (when it's defined) and that's it.

Also, for someone later reading your code, they have to look somewhere
else to find out whether or not intName is really an int, and if not what
were you trying to do? My way, you don't care what type it is... you're
just filling it with as many bytes as it can hold.
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);

I want to maintain a file structure for later reading from the binary file.
So I think i need to, dont I?

No, just count bytes. If you write out spaces, you'll just have to skip
over them when you read. All it does is make more work for yourself.

Josh
 
J

Josh Sebastian

I use fnew = fopen(newFileName, "wb"), but I still can see the content
using notepad or other text editor, so i suppose it is ASCII, but I want to
generate binary file.

Text data looks (for the most part) the same. The only thing that should
change is how numbers look.

Josh
 
E

Eric Sosman

FrancisC said:
[unattributed quotation]
I know (I suppose) how to change the "intName" and "array" using
fwrite( ) as follow:

fwrite(&intName, sizeof(int), 1, fnew);
fwrite(&array, sizeof(double), 1, fnew);


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


why use sizeof (intName) and sizeof (array) instead of sizeof(int) and
sizeof(double) ?
intName and array[] is set as int and double respectively
so are they both correct?


The first form is correct provided that `intName' and
`array' are an `int' and a `double', respectively. Are
they? You cannot tell by looking only at these lines; you
need to hunt for the variable declarations to be sure.

The second form is correct, even if `intName' is actually
a `long' or `short', and even if `array' is a `float' or
a `long double'. It is correct even if the two variables are
`struct IntWrapper' and `struct FourDimensionalPoint'. You
can see immediately that it is correct, without rummaging
around elsewhere in the code.

The general principle is to avoid telling the compiler the
same thing twice, because you might contradict yourself by
mistake. You have already told the compiler what the types
of `intName' and `array' are, and the compiler remembers this
knowledge. The compiler's memory for this sort of fact is
more reliable than your own, so take advantage of it.
 
I

Irrwahn Grausewitz

FrancisC said:
fwrite(&intName, sizeof(int), 1, fnew);
fwrite(&array, sizeof(double), 1, fnew);


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


why use sizeof (intName) and sizeof (array) instead of sizeof(int) and
sizeof(double) ?
intName and array[] is set as int and double respectively
so are they both correct?


Both are correct, but the latter versions are easier to maintain. Think
about what happens if you decide to change the type of intName or array:
in the former versions you have to adjust the operand of sizeof, the
latter will nicely 'auto-adjust'.
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);

I want to maintain a file structure for later reading from the binary file.
So I think i need to, dont I?

No, you don't have to. What you have to do is remember the file
structure ("field width") when you read the file.

BTW, if you want your data files to be reliably portable between
different OSs/implementations you want to stick to the text file
approach (as binary data representations tend to vary across different
systems). Eventually converting text files (if necessary at all) is not
a big deal compared to, for example, translation between different
floating point representations.

Regards
 
I

Irrwahn Grausewitz

SomeDumbGuy said:
Josh Sebastian wrote:

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



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


Sorry to bother you.
In:

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


Note: parantheses around these sizeof operands are redundant.
I understand why you changed the second one.
Is the first more for readability? Or for maintaince?

Both improve maintainability.

Regards
 
I

Irrwahn Grausewitz

FrancisC said:
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

I use fnew = fopen(newFileName, "wb"), but I still can see the content
using notepad or other text editor, so i suppose it is ASCII,

Of course: you wrote text data to a file, what did you expect to see?
but I want to
generate binary file.

You already did; using fprintf, the binary data just happened to be
text. :)

Regards
 
F

FrancisC

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

I use fnew = fopen(newFileName, "wb"), but I still can see the content
using notepad or other text editor, so i suppose it is ASCII,

Of course: you wrote text data to a file, what did you expect to see?
but I want to
generate binary file.

You already did; using fprintf, the binary data just happened to be
text. :)

Thats why I do not want to use fprintf( ) but fwrite( ).
To make all the "things" binary, am I just need to use fwrite( ) all the
times but not using fprintf() ?
I want the filesize as small as possible and as easy for the computer to
read as possible
 
F

FrancisC

fwrite(&intName, sizeof(int), 1, fnew);
fwrite(&array, sizeof(double), 1, fnew);

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


why use sizeof (intName) and sizeof (array) instead of sizeof(int) and
sizeof(double) ?
intName and array[] is set as int and double respectively
so are they both correct?


Both are correct, but the latter versions are easier to maintain. Think
about what happens if you decide to change the type of intName or array:
in the former versions you have to adjust the operand of sizeof, the
latter will nicely 'auto-adjust'.
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);

I want to maintain a file structure for later reading from the binary file.
So I think i need to, dont I?

No, you don't have to. What you have to do is remember the file
structure ("field width") when you read the file.

BTW, if you want your data files to be reliably portable between
different OSs/implementations you want to stick to the text file
approach (as binary data representations tend to vary across different
systems). Eventually converting text files (if necessary at all) is not
a big deal compared to, for example, translation between different
floating point representations.


thats mean if I want to store the file like this one (the integer is the no
of double no below):
3
100000.000000 200000.000000
300000.000000
4
100000.000000 200000.000000
300000.000000 400000.000000

in binary i should store like this with no space and new line?
3100000.000000200000.000000300000.0000004100000.000000200000.000000300000.00
0000400000.000000

when I want to read the file, I use the code below??

while ( !feof(fnew) )
{
fread(&intName, sizeof (intName), 1, fnew);
/*determine the integer value, intVal. (i do not know how to write this
code yet)*/
fread(&doubleNo, sizeof (doubleNo), intVal, fnew);
}
 
A

Al Bowers

FrancisC said:
thats mean if I want to store the file like this one (the integer is the no
of double no below):
3
100000.000000 200000.000000
300000.000000
4
100000.000000 200000.000000
300000.000000 400000.000000

in binary i should store like this with no space and new line?
3100000.000000200000.000000300000.0000004100000.000000200000.000000300000.00
0000400000.000000

when I want to read the file, I use the code below??

while ( !feof(fnew) )
{
fread(&intName, sizeof (intName), 1, fnew);
/*determine the integer value, intVal. (i do not know how to write this
code yet)*/
fread(&doubleNo, sizeof (doubleNo), intVal, fnew);
}

You have been warned that this it is not a portable to store these
values in a binary file. If you insist, here is how you might write
the code.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int count = 3, i;
double darray[3],dd;
FILE *fp;

if((fp = fopen("test.bin","wb")) != NULL)
{ /* Create and write the int and double values to binary file*/
if(1 != fwrite(&count,sizeof count,1,fp)) exit(EXIT_FAILURE);
for(i = 0;i < count;i++)
{
dd = i+1.0;
if(1 != fwrite(&dd,sizeof dd,1,fp)) exit(EXIT_FAILURE);
}
fclose(fp);
/* Read and Test the Binary File */
if((fp = fopen("test.bin","rb")) != NULL)
{ /* read the binary file */
if(1 != fread(&count, sizeof count,1,fp))
exit(EXIT_FAILURE);
for(i = 0;i < dd;i++)
if(1 != fread(&darray, sizeof darray,1,fp))
exit(EXIT_FAILURE);
fclose(fp);
}
}
/* print contents of binary file */
printf("Reading the binary file. there are %d \n"
"type double values in the file\nThery are:\n",count);
for(i = 0; i < count;i++)
printf("darray[%d] = %.2f\n",i,darray);
return 0;
}
 
K

Karl Heinz Buchegger

FrancisC said:
thats mean if I want to store the file like this one (the integer is the no
of double no below):
3
100000.000000 200000.000000
300000.000000
4
100000.000000 200000.000000
300000.000000 400000.000000

in binary i should store like this with no space and new line?
3100000.000000200000.000000300000.0000004100000.000000200000.000000300000.00
0000400000.000000

In binary the above looks completely different, since numbers are stored
in the memory representation at the file. That's what binary means: no
translation when writing to the file, no translation when reading from
the file.

when I want to read the file, I use the code below??

while ( !feof(fnew) )
{
fread(&intName, sizeof (intName), 1, fnew);
/*determine the integer value, intVal. (i do not know how to write this
code yet)*/
fread(&doubleNo, sizeof (doubleNo), intVal, fnew);
}

No.
For 2 reasons:
* You don't use feof to control the loop. That's not what it is ment for.
feof() is used *after* you finish reading to check why the read has stopped.
if it was because of eof, then you have read the whole file. If it was not
because of eof, then some error has occoured.

* It is always a good idea to store some count in the file (just as you have
done it in the text version), that will guide the read process.

int count;
double numbers[100];

/* somehow you have filled the array with the numbers
** and stored a count of how many entries in the array
** you have used in count
**
** now write to file
*/
fwrite( &count, sizeof( count ), 1, fnew );
fwrite( numbers, sizeof( numbers[0] ), count, fnew );


...

/*
** and read from file
*/
fread( &count, sizeof( count ), 1, fnew );
fread( numbers, sizeof( numbers[0] ), count, fnew );


of course error checking needs to be added (use the return values
of fwrite and fread), but basically thats it.
 
V

Villy Kruse

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


Doing that will also write the terminating \0 character to the file.
That is fine if that is what you intend to do.



Villy
 
R

Richard Bos

[ Please do not snip attribution lines for quotes you leave in. While
you're at it, _do_ snip text you're not responding to. ]
Thats why I do not want to use fprintf( ) but fwrite( ).
To make all the "things" binary, am I just need to use fwrite( ) all the
times but not using fprintf() ?

This makes no sense whatsoever. The data you have _is_ text. Writing it
using another function does not magically turn it into non-text. There
is _no_ difference between a byte written using fprintf() and the same
byte written using fwrite().
You seem to be rather confused as to what "binary data" actually is.
Have you considered the existence of the utility called "strings"? Have
you ever actually loaded a "binary" file into a text (or hex) viewer?
I want the filesize as small as possible and as easy for the computer to
read as possible

Those two requirements are mutually exclusive. To make the file as small
as possible, compress it. To make it easily readable, just output the
thing!

Richard
 
M

Mark McIntyre

On Tue, 14 Oct 2003 08:31:45 +0800, in comp.lang.c , "FrancisC"

(lots of unattributed and untrimmed stuff)

Francis, some friendly advice: when responding to posts, please leave
the Attribution lines in (as above, the bit that says "FrancisC
wrote"), and trim your posts to remove irrelevant material.

Leaving in the attributions is important for people trying to follow a
thread, so you know who said which bit.
Trimming is important to ensure that posts to not become too long, and
relevant material appears in them.
 
M

Mark McIntyre

To make all the "things" binary, am I just need to use fwrite( ) all the
times but not using fprintf() ?

fwrite() dumps a block of memory to file. fprintf() prints formatted
data to a file, which might be text, or might not. Which do you want
to do?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top