Which function sets the file position to a specific location?
Answer: A
fseek() is used to move the file pointer to a specific position in the file. It takes three arguments: file pointer, offset, and origin.
Q.342Medium
What does feof() function check?
Answer: A
feof() returns true (non-zero) if the end-of-file indicator is set for the given file stream, otherwise it returns 0.
Q.343Medium
What is the difference between fgetc() and getc()?
Answer: A
fgetc() is a function that reads a character from a file, while getc() is a macro. Both serve the same purpose but getc() may be faster due to macro expansion.
Q.344Medium
Which mode allows both reading and writing without truncating?
Answer: A
"r+" mode opens a file for both reading and writing without truncating it. The file must exist. "w+" creates a new file or truncates existing one.
Q.345Medium
What does fwrite() function return?
Answer: A
fwrite() returns the number of items successfully written to the file, which may be less than the count requested if an error occurs.
Advertisement
Q.346Medium
What is the purpose of rewind() function in file handling?
Answer: A
rewind() moves the file pointer to the beginning of the file and clears the EOF indicator. It's equivalent to fseek(file, 0, SEEK_SET).
Q.347Medium
Consider the code: FILE *fp = fopen("data.txt", "r"); if(fp == NULL) { printf("Error"); } What does this check?
Answer: A
This code checks if fopen() returned NULL, which indicates the file failed to open. This is the standard error checking method for file operations.
Q.348Medium
What happens when fseek() is used with SEEK_END as origin?
Answer: A
SEEK_END moves the file pointer to the end of the file. Offset can be 0 or negative to move before the end. SEEK_SET starts from beginning, SEEK_CUR from current position.
Q.349Medium
Which function is used to read a string from a file, including whitespace characters, up to a maximum length?
Answer: B
fgets() reads a string from file including whitespace until newline or EOF is encountered or max length is reached.
Q.350Medium
In the context of ferror() function, what does it return?
Answer: A
ferror() returns a non-zero value if an error flag is set for the stream, otherwise returns 0.
Q.351Medium
Which mode should be used if you want to read and write to a file without truncating existing content?
Answer: B
The 'r+' mode opens a file for both reading and writing without truncating it. The file must exist.
Q.352Medium
What is the purpose of the clearerr() function in file handling?
Answer: A
clearerr() clears both the EOF indicator and the error flag associated with a stream.
Q.353Medium
Consider a scenario where fopen() fails to open a file. Which of the following is the correct way to handle it?
Answer: C
fopen() returns NULL pointer on failure. Checking 'fp == NULL' or '!fp' is the correct approach. Option C uses explicit NULL check which is clearer.
Q.354Medium
What does ftell() function return?
Answer: B
ftell() returns the current position of the file pointer as a long integer offset from the beginning of the file.
Q.355Medium
Which of the following fseek() calls will position the file pointer at the end of the file?
Answer: C
fseek(fp, 0, SEEK_END) positions the file pointer at the end of file. SEEK_END is the third constant in <stdio.h>.
Q.356Medium
What is the difference between text mode and binary mode when reading files?
Answer: B
In text mode, platform-specific line endings (\r\n on Windows, \n on Unix) are translated. Binary mode reads bytes as-is without translation.
Q.357Medium
Which function is used to read formatted data from a file, similar to scanf() but for files?
Answer: C
fscanf() reads formatted data from a file stream, functioning as the file equivalent of scanf().
Q.358Medium
What will be the behavior of fgets() when reading from an empty line in a file?
Answer: B
fgets() reads up to and including the newline character. For an empty line, it reads just the newline character into the buffer.
Q.359Medium
A program reads a text file line by line using fgets(). If a line contains more characters than the buffer size, what happens?
Answer: A
fgets(buffer, size, fp) reads at most size-1 characters or until newline, whichever comes first, then adds null terminator. Remaining characters stay in stream for next read.
Q.360Medium
A file is opened in binary mode. How will fseek() behave differently compared to text mode?
Answer: C
In text mode, fseek() behavior is implementation-dependent due to line-ending conversions (\r\n vs \n). Binary mode provides more predictable fseek() behavior with absolute byte positions.