'rb' mode opens a file for reading in binary format. Binary mode reads the exact bytes without any text conversion.
Q.682Easy
Which function is used to close a file in C?
Answer: A
fclose() is the standard function to close an opened file stream. It returns 0 on success and EOF on error.
Q.683Easy
Which mode opens a file for writing and truncates it if it exists?
Answer: A
"w" mode opens a file for writing. If the file exists, it truncates the file to zero length. If it doesn't exist, a new file is created.
Q.684Medium
What does fgets() function do?
Answer: A
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.685Medium
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.
Advertisement
Q.686Medium
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.687Medium
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.688Medium
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.689Medium
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.690Medium
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.691Medium
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.692Medium
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.693Medium
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.694Medium
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.695Hard
Write a code snippet to read 100 bytes from a file. Which is correct?
Answer: A
fread() syntax is fread(ptr, size, count, stream). To read 100 bytes, size=1 and count=100. Option B reads 100 items of 1 byte each, which is equivalent but less clear.
Q.696Hard
Which of the following correctly implements appending to a file?
Answer: A
Mode "a" opens a file for appending. New data is written at the end of the file. Mode "w" truncates the file, "r+" requires file to exist, "ab+" is appending in binary with both read/write.
Q.697Easy
When opening a file in 'w' mode, what happens if the file already exists?
Answer: A
Opening a file in 'w' mode truncates the file to zero length if it exists, discarding all previous content.
Q.698Easy
Which of the following file modes in fopen() allows reading only?
Answer: B
The 'r' mode opens a file for reading only. The file must exist, otherwise fopen() returns NULL.
Q.699Easy
In file handling, what does the 'b' flag in file modes like 'rb' or 'wb' indicate?
Answer: A
The 'b' flag specifies binary mode, which prevents any translation of line endings or special characters.
Q.700Medium
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.