Re: Writing an int to a file, not quite sure how buffers work.



<mellyshum123@xxxxxxxx> wrote in message
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);

C files come in binary or text flavours.
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.


.



Relevant Pages

  • Re: Cannot return values of char variable
    ... - buffer = ... Since you seem to be trying to return a char pointer ... int id = random; ... content is interpreted as a string. ...
    (comp.lang.c)
  • Can anyone help me out?
    ... int main{ ... string fname; ... Packet *packet = new Packet; ... packet->package(filename, filesize, buffer); ...
    (comp.unix.programmer)
  • Re: How to split a compressed file programmatically?
    ... value is there in converting that to a string and then back to a long? ... And why allocate a new buffer for each chunk you want to write, ... void splitFile(string path, string path_parts, int size_part) ... just read chunks of the original file until you can't ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to split a compressed file programmatically?
    ... Personally, I would forget about the calculation altogether and just write a loop that keeps writing bytes in chunks as large as you want or however many bytes you have remaining, whichever is less, until you have no more bytes to write. ... The "size_part" variable is already a long; what possible value is there in converting that to a string and then back to a long? ... And why allocate a new buffer for each chunk you want to write, and why does that buffer have to be the length of the original file, and given that you're allocating a new buffer each time, why read the data anywhere other than the beginning of the buffer? ... void splitFile(string path, string path_parts, int size_part) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [RFC][PATCH] Escaping the arguments of kernel messages for sane user-space localisation
    ... escape certain characters in string arguments, ... We get rid of a 1K temporary buffer in printk. ... +static int printed_len; ... +static void printk_begin ...
    (Linux-Kernel)