Can getc()/putc()/fgetc()/fputc() be used to read/write chars and floats rather than ints only from binary files? If so, how?

Dani AI

Generated

: short answer — getc/putc (and fgetc/fputc) operate on single bytes and are fine for reading/writing raw bytes or characters. For multi-byte objects (float, double, structs) prefer block I/O or a clear byte-to-value conversion. rightly pointed to the docs for the byte functions, and is correct that fread/fwrite (or C++ read/write) are simpler and far less error-prone.

Single-byte example (safe EOF check):

int c = getc(fp);
if (c == EOF) { /* handle EOF or error */ }
unsigned char b = (unsigned char)c; /* raw byte / char value */

Why avoid byte-by-byte casts for floats: copying bytes into an object by casting a char[] pointer to float * can violate alignment or strict-aliasing rules on some platforms and cause undefined behaviour. Use fread/fwrite or memcpy to move bytes into/out of typed variables and always check the return value.

If you need portable binary floats (across different endianness or machines), store them in a canonical form. For 32-bit IEEE floats you can copy the bits into a 32-bit integer, convert to network byte order, then write; reverse on read:

uint32_t u;
memcpy(&u, &f, sizeof u);
u = htonl(u);         /* convert byte order for transport */
fwrite(&u, sizeof u, 1, fp);

Notes and troubleshooting:

  • Always open files with "rb"/"wb" for binary on platforms that distinguish text/binary.
  • Check fread/fwrite return counts and use ferror/feof.
  • Verify sizeof(float)==4 and IEEE-754 assumptions before using integer-byte tricks.
  • For long-term or cross-platform storage prefer text format or a standard serializer (XDR, Protocol Buffers, etc.).

Recommended Answers

All 3 Replies

Yes. you can read more about it here

Yes you can use it, but I won't suggest it. Its a whole lot easier to use fread() and fwrite() to read/write to/from binary files.

// fgetc example
float f = 0;
unsigned char buf[sizeof(float)];
FILE* fp = fopen("myfile.dat", "rb");
// read a float
for(i = 0; i < sizeof(float); i++)
   buf[i] = fgetc(fp);
// move the contents of buf into the float variable
f = *(float *)buf;

Here is the same thing using fread

float f;
FILE* fp = fopen("myfile.dat", "rb");
// read the float
fread((char *)&f, sizeof(float),1,fp);

fread() is a lot less typing, lot less error prone, and more straight-forward.

Yes you can use it, but I won't suggest it. Its a whole lot easier to use fread() and fwrite() to read/write to/from binary files.

// fgetc example
float f = 0;
unsigned char buf[sizeof(float)];
FILE* fp = fopen("myfile.dat", "rb");
// read a float
for(i = 0; i < sizeof(float); i++)
   buf[i] = fgetc(fp);
// move the contents of buf into the float variable
f = *(float *)buf;

Thank you very much, I didn't realize it would be that complicated to read with fgetc(). You're right, fread() is much better :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.