In C, consider the following code that writes a structure to a file using fwrite: struct emp { char *n; int age; }; struct emp e = { "Sujay", 15 }; FILE *fp; /* assume fp is a valid file pointer opened in binary write mode */ fwrite(&e, sizeof(e), 1, fp); Can another program later call fread on this file to reliably reconstruct the same struct emp value, including the string "Sujay", using a single fread(&e, sizeof(e), 1, fp) call?

Difficulty: Medium

Correct Answer: No, because only the pointer value n is written, not the characters of the string "Sujay", so reading it back will not reliably reconstruct the original string

Explanation:


Introduction / Context:
Binary file I/O in C using fwrite and fread is often used to persist structures to disk. However, when structures contain pointers, simply writing the structure memory does not automatically serialize the data pointed to. Understanding this distinction is critical for designing portable and correct file formats.



Given Data / Assumptions:

  • The structure emp contains a pointer field char *n and an int field age.
  • The instance e initializes n to point to the string literal "Sujay".
  • fwrite(&e, sizeof(e), 1, fp); writes the raw bytes of the struct e to the file.
  • We want to know whether fread can later read the struct back and recover the original string correctly.


Concept / Approach:
The expression fwrite(&e, sizeof(e), 1, fp); writes the in-memory representation of the struct, including the numeric value stored in the pointer n and the integer age. It does not write the characters of the string "Sujay" themselves, only the address of that string in the current process address space. When the file is read back, the raw pointer value is restored, but this numeric address will not correspond to a valid string in the new process. Therefore, the reconstructed structure will contain an invalid pointer, and the original string data is not reliably preserved.



Step-by-Step Solution:
Step 1: Recognize that n is a pointer, not an embedded character array.Step 2: During fwrite, the bytes corresponding to the pointer n and the integer age are written.Step 3: The actual characters 'S', 'u', 'j', 'a', 'y', and the null terminator reside in program memory as part of a string literal and are not written by this fwrite call.Step 4: Upon fread, the pointer value n is restored as a numeric address, but in the new run this address is almost certainly meaningless or invalid.Step 5: Therefore, the original string "Sujay" cannot be reliably reconstructed using a single fread of this structure.


Verification / Alternative check:
Practical experience shows that writing pointer-containing structures with fwrite leads to portability and correctness problems. Texts on C file I/O recommend that only structures with fixed-size, non-pointer members (for example, char arrays, ints, doubles) be written with a simple fwrite, or that custom serialization be implemented to write and read pointed-to data separately. This supports the conclusion that the given code does not robustly serialize the string.



Why Other Options Are Wrong:
Option B is incorrect because pointer values are process-specific addresses; they have no meaning across different executions or machines. Option C is wrong because the issue is not the size of int, but the presence of a pointer that is not dereferenced during I/O. Option D is incorrect because fread can certainly read structures composed of simple, non-pointer fields; it is the presence of pointers that makes this case unsafe.



Common Pitfalls:
A common mistake is to assume that fwrite and fread magically handle all nested data, including what pointers reference. In reality, binary serialization must explicitly manage any dynamically allocated or pointed-to memory. Another pitfall is testing such code only in a single run where addresses coincidentally look valid, hiding the underlying bug until later.



Final Answer:
The structure cannot be safely reconstructed with a single fread, because only the pointer value n is written, not the bytes of the string "Sujay", so reading it back does not reliably restore the original string.


More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion