What does the function ftell(fp) return in file handling?
Answer: A
ftell(fp) returns the current position of the file pointer (in bytes) from the beginning of the file. Returns -1L on error.
Q.62Medium
Which of the following correctly reads a string from file with size limit?
Answer: A
fgets(buffer, size, fp) safely reads up to (size-1) characters or until newline from file fp, preventing buffer overflow. fscanf() is vulnerable to overflow.
Q.63Medium
A program needs to update specific bytes in the middle of a file without affecting other data. Which approach is most suitable?
Answer: A
Using fseek() with r+ or w+ mode allows direct access to specific file positions. This is efficient for small modifications without rewriting the entire file.
Q.64Medium
What is the difference between 'rb' and 'r' modes when opening a file?
Answer: A
'rb' (binary read) reads data as-is without converting line endings. 'r' (text read) converts platform-specific newlines (\r\n on Windows) to \n.
Q.65Medium
In a file opened with 'rb+' mode, what operations can be performed?
Answer: A
'rb+' opens a binary file for both reading and writing. The file must exist. Existing content is preserved and can be overwritten at any position.
Advertisement
Q.66Medium
A program reads structured data: fread(ptr, sizeof(struct), count, fp). If fread() returns a value less than 'count', what could be the reason?
Answer: A
fread() returns the number of items actually read. If less than 'count' is returned, it indicates EOF was reached or a read error occurred before completing the request.
Q.67Hard
How can you determine the size of a file in bytes using standard C functions?
Answer: D
Multiple methods exist: fseek/ftell repositions and returns position, stat() gets file info including size, and fstat() does similar with file descriptor. All are valid.
Q.68Hard
When writing binary structures with fwrite(), which precaution is essential for portability across different systems?
Answer: A
Struct padding varies by architecture and compiler. Binary files written on one system may not read correctly on another due to different alignment. Use serialization techniques for portability.
Q.69Hard
A program processes a file with multiple reading and writing operations without closing/reopening. What is a potential issue with file buffering?
Answer: A
File I/O uses buffering for efficiency. Data in write buffer stays in memory until fflush(fp) explicitly flushes or fclose() is called, risking data loss if program crashes.
Q.70Medium
In competitive exam file handling questions, if fseek() is called with SEEK_CUR, what does it indicate?
Answer: A
SEEK_CUR is a constant meaning 'current position'. fseek(fp, offset, SEEK_CUR) moves the pointer by 'offset' bytes from its current position.
Q.71Medium
A C program must securely close a file and handle potential errors. What is the best approach?
Answer: A
fclose() returns 0 on success and EOF (-1) on error. Checking return value ensures buffered data is flushed and handles errors like write failures.
Q.72Hard
Which scenario would be most problematic when using fprintf() for data that needs exact binary preservation?
Answer: D
fprintf() is text-oriented and interprets format specifiers, potentially modifying data. It also stops at NULL characters. For binary data, fwrite() is appropriate.
Q.73Hard
For reading a configuration file line-by-line where lines may exceed 256 characters, which function is recommended for ISRO/GATE 2024 exams?
Answer: A
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.
Q.74Easy
Which file opening mode allows both reading and writing without truncating existing content?
Answer: A
Mode 'r+' opens file for reading and writing without truncating. 'w+' truncates the file, 'a+' appends, and 'x+' is for exclusive creation.
Q.75Easy
What does fseek(fp, 0, SEEK_END) accomplish?
Answer: B
SEEK_END moves the file pointer to the end of file. SEEK_SET goes to beginning, SEEK_CUR to current position.
Q.76Easy
Which function returns the current position of file pointer?
Answer: B
ftell() returns the current file position as a long integer. fgetpos() stores position in a fpos_t structure. rewind() resets to beginning.
Q.77Easy
Which mode opens file in text format with write permission only?
Answer: B
'w' mode opens file in text format for writing. 'wb' is binary write. 'wt' is explicitly text write but 'w' is default text mode.
Q.78Medium
A program needs to append 500 integers to an existing binary file. Which combination is correct?
Answer: A
Binary append requires 'ab' mode with fwrite(). Option B uses text mode. Option D would overwrite. Option C mixes binary mode with text function.
Q.79Medium
What happens if you call fclose() on an already closed file pointer?
Answer: B
Calling fclose() on closed file causes undefined behavior and returns EOF. This is a runtime issue, not compile-time.
Q.80Medium
A program reads struct records using fread(). After reading 100 records of 64 bytes each, what is the total bytes read if successful?
Answer: C
fread(ptr, 64, 100, fp) reads 100 items of 64 bytes each = 6400 bytes total. fread() returns count of items read, not bytes.