File Handling Question Bank for C-CAT
Topic-wise File Handling MCQs for CDAC C-CAT preparation with answers and explanations.
Show Answer & Explanation
Correct Answer: C - fopen()
fopen() is the standard library function used to open a file in C. It returns a FILE pointer.
Show Answer & Explanation
Correct Answer: D - Read and write
The "r+" mode opens a file for both reading and writing. The file must exist.
Show Answer & Explanation
Correct Answer: A - fclose()
fclose() is used to close a file that was opened with fopen().
Show Answer & Explanation
Correct Answer: B - End of file
feof() returns non-zero if the end-of-file indicator is set for the stream.
Show Answer & Explanation
Correct Answer: D - Both a and b
Both putc() and fputc() can be used to write a character to a file. They are essentially the same, with putc() possibly being a macro.
Show Answer & Explanation
Correct Answer: B - Opens for writing (creates/truncates)
The "w" mode opens a file for writing. If the file exists, it is truncated. If it does not exist, a new file is created.
Show Answer & Explanation
Correct Answer: A - fgets()
fgets() reads a line (up to specified number of characters or newline) from a file into a string.
Show Answer & Explanation
Correct Answer: D - NULL
fopen() returns NULL if it fails to open the file, which should always be checked before file operations.
Show Answer & Explanation
Correct Answer: B - Append mode
The "a" mode opens a file for appending. Data is written at the end of the file without truncating existing content.
Show Answer & Explanation
Correct Answer: D - fseek()
fseek() is used to move the file pointer to a specific position. It takes file pointer, offset, and origin as arguments.
Show Answer & Explanation
Correct Answer: B - Current position of file pointer
ftell() returns the current position of the file pointer, which indicates how many bytes from the beginning of the file.
Show Answer & Explanation
Correct Answer: B - Moves pointer to beginning
rewind() moves the file pointer back to the beginning of the file. It is equivalent to fseek(fp, 0, SEEK_SET).
Show Answer & Explanation
Correct Answer: D - SEEK_SET
SEEK_SET is used to seek from the beginning of the file. SEEK_CUR is from current position, SEEK_END is from end.
Show Answer & Explanation
Correct Answer: C - fprintf() is formatted, fputs() is unformatted
fprintf() allows formatted output (like printf) to a file, while fputs() writes a string as-is without formatting.
Show Answer & Explanation
Correct Answer: B - Reads a block of data
fread() reads a specified number of blocks of a given size from a file into a buffer. Used for binary file reading.
Show Answer & Explanation
Correct Answer: B - w+
w+ opens for reading and writing. If file exists, it is truncated. If not, a new file is created.
Show Answer & Explanation
Correct Answer: B - Flush the output buffer
fflush() forces any buffered data to be written to the file immediately, without waiting for the buffer to fill.
Show Answer & Explanation
Correct Answer: D - b
Adding b to the mode string, as in rb or wb, opens a file in binary mode and avoids text newline translation.
Show Answer & Explanation
Correct Answer: C - fwrite()
fwrite() writes a block of data (specified number of elements of specified size) to a file. Used for binary files.
Show Answer & Explanation
Correct Answer: A - Undefined behavior/error
"w" mode is write-only. Trying to read from a file opened in "w" mode results in undefined behavior or error.
Show Answer & Explanation
Correct Answer: D - fopen()
fopen() is the standard C library function used to open files. It takes a filename and mode string, returning a FILE pointer.
Show Answer & Explanation
Correct Answer: B - Opens an existing file for reading
The mode "r" opens an existing file for reading only. If the file does not exist, fopen() returns NULL.
Show Answer & Explanation
Correct Answer: C - NULL
fopen() returns NULL if the file cannot be opened. It is good practice to always check the return value before proceeding with file operations.
Show Answer & Explanation
Correct Answer: B - Truncates the file to zero length
The "w" mode creates a new file or truncates an existing file to zero length. All previous contents are lost.
What is the output?
FILE *fp = fopen("test.txt", "w");
fprintf(fp, "Score: %d", 95);
fclose(fp);What is written to test.txt?
Show Answer & Explanation
Correct Answer: A - Score: 95
fprintf() works like printf() but writes to a file. It formats the output, replacing %d with 95, writing "Score: 95" to the file.
Show Answer & Explanation
Correct Answer: B - fgetc()
fgetc() reads and returns a single character from the file. It returns EOF when the end of file is reached.
Show Answer & Explanation
Correct Answer: C - 0
fclose() returns 0 on success and EOF on failure. It flushes the buffer and releases the file resource.
Show Answer & Explanation
Correct Answer: D - "a" appends data, "w" overwrites the file
Mode "a" opens a file for appending; data is added to the end without erasing existing content. Mode "w" truncates the file, destroying existing content.
What does this code do?
FILE *fp = fopen("data.bin", "rb");
int arr[5];
fread(arr, sizeof(int), 5, fp);
fclose(fp);Show Answer & Explanation
Correct Answer: C - Reads 5 integers from a binary file into arr
fread() reads binary data. Here it reads 5 blocks of sizeof(int) bytes each from the file into the array arr. Mode "rb" opens the file for binary reading.
Show Answer & Explanation
Correct Answer: A - size_t fwrite(const void *ptr, size_t size, size_t count, FILE *fp)
fwrite() takes a pointer to data, the size of each element, the number of elements, and the file pointer. It returns the number of elements successfully written.
Show Answer & Explanation
Correct Answer: D - The current position of the file pointer
ftell() returns the current position of the file pointer as a long integer, measured in bytes from the beginning of the file.
Show Answer & Explanation
Correct Answer: A - Moves the file pointer to the beginning
rewind() sets the file position indicator to the beginning of the file. It is equivalent to fseek(fp, 0, SEEK_SET) but also clears the error indicator.
Show Answer & Explanation
Correct Answer: C - Moves to the end of the file
fseek(fp, 0, SEEK_END) moves the file pointer 0 bytes from the end of the file, effectively positioning it at the end.
What is the output?
FILE *fp = fopen("test.txt", "w");
fputs("Hello\n", fp);
fputs("World\n", fp);
fclose(fp);What does test.txt contain?
Show Answer & Explanation
Correct Answer: A - Hello and World on separate lines
fputs() writes strings to the file. The \n characters create newlines, so "Hello" and "World" appear on separate lines.
Show Answer & Explanation
Correct Answer: B - r+
Mode "r+" opens an existing file for both reading and writing without truncating. "w+" would truncate. "a+" opens for reading and appending.
Show Answer & Explanation
Correct Answer: A - If the end of file has been reached
feof() returns a non-zero value if the end-of-file indicator is set for the stream. It is often used in loops to detect when all data has been read.
What is the problem with this code?
FILE *fp = fopen("data.txt", "r");
char line[100];
while(!feof(fp)) {
fgets(line, 100, fp);
printf("%s", line);
}
fclose(fp);Show Answer & Explanation
Correct Answer: A - The last line may be printed twice
feof() returns true only after a read attempt fails. After reading the last line, feof() is still false, so fgets() is called again and fails, but the previous line is printed again.
Show Answer & Explanation
Correct Answer: C - Binary mode
The "b" suffix indicates binary mode. In binary mode, no character translations (like newline conversion) are performed, which is important for non-text files.
What is the correct way to read a line from a file?
FILE *fp = fopen("data.txt", "r");
char buf[256];Show Answer & Explanation
Correct Answer: C - fgets(buf, 256, fp);
fgets(buf, 256, fp) reads up to 255 characters or until a newline or EOF, whichever comes first, and stores the result in buf with a null terminator.
Show Answer & Explanation
Correct Answer: D - The number of items successfully matched and assigned
fscanf() returns the number of input items successfully matched and assigned. It returns EOF if an input failure occurs before any conversion.
Show Answer & Explanation
Correct Answer: C - Using fseek() to end and ftell()
A common approach is fseek(fp, 0, SEEK_END) to move to the end, then ftell(fp) to get the position (which equals the file size in bytes).
Show Answer & Explanation
Correct Answer: A - Deletes the file from disk
remove() deletes the file whose name is specified. It returns 0 on success and non-zero on failure.
What does rename() do in C?
rename("old.txt", "new.txt");Show Answer & Explanation
Correct Answer: C - Renames a file on disk
rename() renames the file from the first argument to the second argument. It returns 0 on success and non-zero on failure.
What is the correct way to write a structure to a binary file?
struct Record { int id; char name[20]; };
struct Record r = {1, "Test"};
FILE *fp = fopen("data.bin", "wb");Show Answer & Explanation
Correct Answer: A - fwrite(&r, sizeof(struct Record), 1, fp);
fwrite() writes binary data. Writing &r with sizeof(struct Record) bytes writes the entire structure to the file in binary format.
Show Answer & Explanation
Correct Answer: D - In text mode, newline characters may be translated; in binary mode they are not
In text mode on Windows, '\n' is translated to '\r\n' on write and back on read. Binary mode performs no such translations, preserving exact byte content.
Show Answer & Explanation
Correct Answer: C - Creates and opens a temporary file that is automatically deleted when closed
tmpfile() creates a temporary binary file in "wb+" mode. The file is automatically deleted when it is closed or the program ends.
What is the output?
FILE *fp = fopen("nums.txt", "w");
for(int i = 1; i <= 5; i++)
fprintf(fp, "%d ", i * 10);
fclose(fp);What is in nums.txt?
Show Answer & Explanation
Correct Answer: A - 10 20 30 40 50
The loop writes i*10 for i from 1 to 5 with spaces, producing "10 20 30 40 50 " in the file.
Show Answer & Explanation
Correct Answer: A - Forces buffered output to be written to the file
fflush() forces any buffered but unwritten data to be written to the output file. It is useful to ensure data is saved immediately.
Show Answer & Explanation
Correct Answer: D - fopen() returns NULL
Mode "r" requires the file to exist. If it does not exist, fopen() returns NULL. You should always check the return value before using the file pointer.
What is the purpose of the third parameter in fseek()?
fseek(fp, offset, whence);Show Answer & Explanation
Correct Answer: B - The reference point: SEEK_SET (beginning), SEEK_CUR (current), or SEEK_END (end)
The third parameter (whence) specifies the reference position for the offset: SEEK_SET (beginning of file), SEEK_CUR (current position), or SEEK_END (end of file).