Home Subjects C Programming File Handling

C Programming
File Handling

C language from basics to advanced placement prep

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 61–70 of 100
Topics in C Programming
Q.61 Easy File Handling
Which function writes formatted output to a file?
A fscanf()
B fprintf()
C fwrite()
D fputs()
Correct Answer:  B. fprintf()
EXPLANATION

fprintf() writes formatted data to file (like printf() but to file). fscanf() reads formatted data. fwrite() writes binary data. fputs() writes strings.

Take Test
Q.62 Medium File Handling
A program reads a text file line by line using fgets(). If a line contains more characters than the buffer size, what happens?
A fgets() reads only up to buffer size - 1 characters and adds null terminator
B fgets() reads entire line regardless of buffer size
C An error occurs and program crashes
D fgets() skips that line entirely
Correct Answer:  A. fgets() reads only up to buffer size - 1 characters and adds null terminator
EXPLANATION

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.

Take Test
Q.63 Easy File Handling
What does the fclose() function do?
A Clears the file content
B Closes the file and flushes the buffer
C Moves file pointer to end
D Deletes the file from disk
Correct Answer:  B. Closes the file and flushes the buffer
EXPLANATION

fclose() terminates the file stream, flushes any buffered data to disk, and releases the file handle. It doesn't delete or clear the file.

Take Test
Q.64 Easy File Handling
Which mode allows both reading and writing to a file, creating it if it doesn't exist?
A "r+"
B "w+"
C "a+"
D "rw"
Correct Answer:  B. "w+"
EXPLANATION

"w+" opens file for reading and writing, truncating it if it exists, and creating it if it doesn't. "r+" requires file to exist. "a+" opens in append mode with read capability.

Take Test
Q.65 Easy File Handling
What is the correct syntax to open a file named 'data.txt' in read mode?
A fopen('data.txt', 'r')
B fopen("data.txt", "r")
C open("data.txt", READ)
D file_open("data.txt", 'r')
Correct Answer:  B. fopen("data.txt", "r")
EXPLANATION

fopen() uses double quotes for string arguments in C. The first argument is filename and second is mode. Single quotes are for characters, not strings.

Take Test
Q.66 Easy File Handling
Which function is used to determine the current position in a file?
A ftell()
B fseek()
C rewind()
D fgetpos()
Correct Answer:  A. ftell()
EXPLANATION

ftell() returns the current position (as a long integer) of the file pointer in the file. fseek() is used to change position, rewind() resets to beginning, and fgetpos() stores position in a structure.

Take Test
Q.67 Hard File Handling
What is the purpose of using fflush() in file handling, and when is it critical to use it?
A To move file pointer to the beginning of the file
B To write buffered data to the actual file on disk
C To close the file gracefully
D To clear error flags from the stream
Correct Answer:  B. To write buffered data to the actual file on disk
EXPLANATION

fflush() writes any buffered data to the underlying file. It's critical when you need to ensure data is written to disk before continuing, especially before reading the same file.

Take Test
Q.68 Hard File Handling
A program writes data using fprintf() and later attempts to read it back. However, the read operation fails intermittently. What could be the most likely cause?
A fprintf() is not suitable for output operations
B The buffer was not flushed before reading, causing incomplete data
C The file was opened in binary mode instead of text mode
D The file pointer was not reset after writing
Correct Answer:  B. The buffer was not flushed before reading, causing incomplete data
EXPLANATION

fprintf() buffers output. Without flushing (fflush() or fclose()), subsequent read operations may see incomplete data. This is a common source of bugs.

Take Test
Q.69 Hard File Handling
Consider using fseek() on a file opened in text mode with SEEK_END and a non-zero offset. What is the standard behavior?
A It is guaranteed to work correctly on all platforms
B It is undefined behavior and may not work correctly on all platforms
C It always returns an error
D It works only on Windows systems
Correct Answer:  B. It is undefined behavior and may not work correctly on all platforms
EXPLANATION

In text mode, fseek() with non-zero offset relative to SEEK_END is undefined behavior. Use fseek(fp, 0, SEEK_END) for reliable behavior.

Take Test
Q.70 Hard File Handling
In a program reading a binary file of 1000 integers, fread() is called as: fread(arr, sizeof(int), 1000, fp). If only 500 integers are present, what will fread() return?
A 500
B An error code
C NULL
D The bytes read (typically 2000)
Correct Answer:  A. 500
EXPLANATION

fread() returns the number of complete items successfully read, not the number of bytes. It will return 500 even if 1000 were requested.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips