For reading a configuration file line-by-line where lines may exceed 256 characters, which function is recommended for ISRO/GATE 2024 exams?
Afgets(buffer, sizeof(buffer), fp) with dynamic memory if needed
Bfscanf(fp, "%s", buffer) with fixed-size array
Cfgetc() in a loop to manually read lines
Dfread() to read entire file at once
Correct Answer:
A. fgets(buffer, sizeof(buffer), fp) with dynamic memory if needed
EXPLANATION
fgets() is safest for line reading with size protection. For lines exceeding buffer size, use dynamic allocation or getline(). fscanf() with %s is vulnerable to buffer overflow.
Which scenario would be most problematic when using fprintf() for data that needs exact binary preservation?
Afprintf() interprets format specifiers and may alter binary data representation
Bfprintf() is always safer than fwrite()
Cfprintf() cannot handle NULL characters in data
DBoth A and C are correct
Correct Answer:
D. Both A and C are correct
EXPLANATION
fprintf() is text-oriented and interprets format specifiers, potentially modifying data. It also stops at NULL characters. For binary data, fwrite() is appropriate.