fgets() reads a string from a file until a newline character or end of file is encountered. It takes three arguments: buffer, size, and file pointer.
Q.2Medium
Which function writes formatted data to a file?
Answer: A
fprintf() writes formatted data to a file, similar to printf() but with a file pointer as the first argument.
Q.3Medium
What is the purpose of ftell() function?
Answer: A
ftell() returns the current position of the file pointer in bytes from the beginning of the file.
Q.4Medium
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.5Medium
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.
Advertisement
Q.6Medium
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.7Medium
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.8Medium
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.
Q.9Medium
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.10Medium
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.11Medium
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.12Medium
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.13Medium
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.14Medium
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.15Medium
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.16Medium
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.17Medium
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.18Medium
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.19Medium
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.20Medium
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().