fopen() is the standard C library function used to open a file. It takes two arguments: filename and mode.
Q.2Easy
What does the mode 'rb' mean in fopen()?
Answer: A
'rb' mode opens a file for reading in binary format. Binary mode reads the exact bytes without any text conversion.
Q.3Easy
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.4Easy
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.5Medium
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.
Advertisement
Q.6Medium
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.7Medium
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.8Medium
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.9Medium
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.10Medium
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.11Medium
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.12Medium
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.13Medium
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.14Medium
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.15Medium
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.16Hard
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.17Hard
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.18Easy
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.19Easy
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.20Easy
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.