QUESTION 05
What is one difference between text and binary streams?
2 marksAnswer
Text streams may translate representations such as line endings. Binary mode is used when the program needs the implementation’s binary-stream behaviour.
QUESTION 05
Text streams may translate representations such as line endings. Binary mode is used when the program needs the implementation’s binary-stream behaviour.
QUESTION 06
Text files represent data as readable character sequences. Binary files can store a specified byte representation, but portability depends on that representation.
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.
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.
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.
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.
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.