Re: Writing an int to a file, not quite sure how buffers work.
- From: "Malcolm" <regniztar@xxxxxxxxxxxxxx>
- Date: Sat, 25 Nov 2006 09:13:28 -0000
<mellyshum123@xxxxxxxx> wrote in message
C files come in binary or text flavours.Why don't you use fprintf?
--
Ian Collins.
Ok. I still don't know how the buffer works. Does it print out the
entire buffer or just the int I have placed in it?
Supposing I wrote:
fprintf(outputfile, "%d",buff);
To write an integer to a binary file
fwrite(&x, sizeof(int), 1, fp);
This will write the bit pattern of the int to the file.
To do it in a machine-independent way
fputc( (x >> 8) & 0xFF, fp);
fputc( x & 0xFF, fp);
This will write a 16-bit big-endian integer to the file. Obviously you might
want to rewrite to make integers little-endian, or 32 bits.
Text files only understnad characters, almost always ASCII. So you have to
write the string "1234" to represent the integer 1234.
However fwrite(fp, "%d", x)) will do the conversion for you.
if you prefer you can write
char buff[64];
sprintf(buff, "%d", x);
fprintf(fp, "%s", buff);
using fprintf to write as a string.
Or you could use fwrite
fwrite(buff, 1, strlen(buff), fp);
There is not normally much point in doing this, unless you want your own
integer-to string conversion routine, for instance to write in binary rather
than decimal.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
.
- Follow-Ups:
- Re: Writing an int to a file, not quite sure how buffers work.
- From: Stephen Sprunk
- Re: Writing an int to a file, not quite sure how buffers work.
- From: Barry Schwarz
- Re: Writing an int to a file, not quite sure how buffers work.
- References:
- Writing an int to a file, not quite sure how buffers work.
- From: mellyshum123
- Re: Writing an int to a file, not quite sure how buffers work.
- From: Ian Collins
- Re: Writing an int to a file, not quite sure how buffers work.
- From: mellyshum123
- Writing an int to a file, not quite sure how buffers work.
- Prev by Date: Re: scanf behaviour
- Next by Date: Re: drawing a line
- Previous by thread: Re: Writing an int to a file, not quite sure how buffers work.
- Next by thread: Re: Writing an int to a file, not quite sure how buffers work.
- Index(es):
Relevant Pages
|