Skip to study material
← Question index

QUESTION 06

Compare text and binary file processing.

5 marks

Answer in brief

Text files represent data as readable character sequences. Binary files can store a specified byte representation, but portability depends on that representation.

Text example

fprintf(fp, "%d ", count) stores a textual number. A reader interprets those characters using a suitable conversion. Text is easy to inspect, but parsing rules, separators and invalid input handling must be defined.

Binary example

fwrite(values, sizeof values[0], n, fp) writes n int objects using their native representations. fread can recover them on the same compatible implementation. This is a teaching example, not a portable interchange format across arbitrary compilers and machines.

Portability

Native representations can vary in integer sizes, byte order, padding and floating-point formats. Writing a whole structure may include padding bytes. A portable binary format must define field widths and encoding explicitly; saving pointer values does not save their targets or make valid pointers for a later process.

Modes and results

Use rb or wb for binary examples. Check element counts returned by fread and fwrite. A short read may be normal end or an error; ferror helps distinguish errors. Always respect the number of elements actually obtained.

Choosing and cost

Prefer readable text when inspection and editing matter. Use a deliberately designed binary representation when compactness and direct record access are useful. Both require work proportional to the data processed; binary mode alone does not guarantee every operation is faster.