Showing 71–80 of 100 questions
in File Handling
What will be the behavior of fgets() when reading from an empty line in a file?
A
It will return NULL
B
It will read the newline character into the buffer
C
It will skip the empty line and read the next non-empty line
D
It will cause a segmentation fault
Correct Answer:
B. It will read the newline character into the buffer
EXPLANATION
fgets() reads up to and including the newline character. For an empty line, it reads just the newline character into the buffer.
Which function is used to read formatted data from a file, similar to scanf() but for files?
A
fread()
B
fgets()
C
fscanf()
D
fgetc()
Correct Answer:
C. fscanf()
EXPLANATION
fscanf() reads formatted data from a file stream, functioning as the file equivalent of scanf().
What is the difference between text mode and binary mode when reading files?
A
Binary mode is faster than text mode
B
Text mode performs line-ending translation, binary mode does not
C
Binary mode can only handle numeric data
D
Text mode is only for ASCII files, binary for Unicode
Correct Answer:
B. Text mode performs line-ending translation, binary mode does not
EXPLANATION
In text mode, platform-specific line endings (\r\n on Windows, \n on Unix) are translated. Binary mode reads bytes as-is without translation.
Which of the following fseek() calls will position the file pointer at the end of the file?
A
fseek(fp, 0, SEEK_SET)
B
fseek(fp, -1, SEEK_END)
C
fseek(fp, 0, SEEK_END)
D
fseek(fp, 1, SEEK_CUR)
Correct Answer:
C. fseek(fp, 0, SEEK_END)
EXPLANATION
fseek(fp, 0, SEEK_END) positions the file pointer at the end of file. SEEK_END is the third constant in <stdio.h>.
What does ftell() function return?
A
The total size of the file in bytes
B
The current position of the file pointer
C
The number of bytes read so far
D
The line number of current file pointer position
Correct Answer:
B. The current position of the file pointer
EXPLANATION
ftell() returns the current position of the file pointer as a long integer offset from the beginning of the file.
Consider a scenario where fopen() fails to open a file. Which of the following is the correct way to handle it?
A
FILE *fp = fopen("file.txt", "r"); if(fp == 0) { /* handle error */ }
B
FILE *fp = fopen("file.txt", "r"); if(fp) { /* process file */ }
C
FILE *fp = fopen("file.txt", "r"); if(fp == NULL) { /* handle error */ }
D
FILE *fp = fopen("file.txt", "r"); if(!fp) { perror("Error"); }
Correct Answer:
C. FILE *fp = fopen("file.txt", "r"); if(fp == NULL) { /* handle error */ }
EXPLANATION
fopen() returns NULL pointer on failure. Checking 'fp == NULL' or '!fp' is the correct approach. Option C uses explicit NULL check which is clearer.
What is the purpose of the clearerr() function in file handling?
A
To clear the EOF indicator and error flags for a stream
B
To delete the file associated with the stream
C
To flush the buffer to disk
D
To clear only the error flag but not EOF
Correct Answer:
A. To clear the EOF indicator and error flags for a stream
EXPLANATION
clearerr() clears both the EOF indicator and the error flag associated with a stream.
Which mode should be used if you want to read and write to a file without truncating existing content?
A
"w+"
B
"r+"
C
"a+"
D
"rb+"
EXPLANATION
The 'r+' mode opens a file for both reading and writing without truncating it. The file must exist.
In the context of ferror() function, what does it return?
A
Non-zero if an error has occurred, zero otherwise
B
The specific error code from the system
C
The line number where error occurred
D
Always returns 0 for binary files
Correct Answer:
A. Non-zero if an error has occurred, zero otherwise
EXPLANATION
ferror() returns a non-zero value if an error flag is set for the stream, otherwise returns 0.
Which function is used to read a string from a file, including whitespace characters, up to a maximum length?
A
fscanf()
B
fgets()
C
getc()
D
fread()
Correct Answer:
B. fgets()
EXPLANATION
fgets() reads a string from file including whitespace until newline or EOF is encountered or max length is reached.