A program uses fprintf() to write 1000 integers to a file but doesn't call fclose(). What could happen?
Answer: B
Without fclose() or fflush(), buffered data remains in memory. If program terminates, that data never reaches disk. System may flush on exit but it's unreliable.
Q.362Medium
A binary file contains 500 structures of size 32 bytes each. To skip the first 10 structures and read the 11th one, which fseek() call is correct?
Answer: B
To skip first 10 structures and read 11th: 10 * 32 = 320 bytes from start. Option B (320 bytes) positions pointer at start of 11th structure (320 bytes offset).
Q.363Medium
Which of the following modes will truncate an existing file?
Answer: C
"w" mode opens file for writing and truncates it to zero length if it exists. "r+" doesn't truncate. "a" and "a+" are append modes that don't truncate.
Q.364Medium
A program reads a CSV file with fgets() storing data in a 256-byte buffer. Some lines exceed 255 characters. How should this be handled?
Answer: A
Best solution is allocate larger buffer. If line is 500 chars, use buffer >= 501. Option C partially works but complicates newline handling. fscanf() doesn't automatically handle long lines.
Q.365Medium
What does fflush(fp) do when fp is a file pointer?
Answer: B
fflush(fp) forces buffered output to be written to disk immediately without closing the file. Useful for ensuring data safety before risky operations.
Advertisement
Q.366Medium
In a file opened in "a+" mode, where does the file pointer initially point?
Answer: B
In append mode "a" or "a+", file pointer initially points to end of file for writing. For reading in "a+", you must explicitly seek to read from start.
Q.367Medium
Which mode allows both reading and appending without truncating existing content?
Answer: A
'a+' mode opens file for reading and appending. New data is added at the end without removing existing content. 'w+' would truncate the file.
Q.368Medium
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.369Medium
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.370Medium
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.371Medium
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.372Medium
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.
Q.373Medium
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.374Medium
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.375Medium
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.376Medium
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.377Medium
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.378Medium
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.
Q.379Medium
Which statement correctly counts non-comment lines in a text file?
Answer: C
Pure C solution uses fgets() to read lines and strstr() or manual checking for comment patterns. Option B uses external tool. Option D is inefficient.
Q.380Medium
In a program processing a CSV file, what is the most robust approach for reading variable-length lines?
Answer: B
Dynamic allocation with getc() handles variable-length lines robustly. fgets() with fixed buffer may truncate. fscanf() with %s doesn't handle spaces. Option D wastes memory.