Home Subjects C Programming File Handling

C Programming
File Handling

C language from basics to advanced placement prep

27 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 11–20 of 27
Topics in C Programming
Q.11 Easy File Handling
Which header file must be included to use file handling functions in C?
A #include
B #include
C #include
D #include
Correct Answer:  A. #include
EXPLANATION

stdio.h (Standard Input/Output) contains all file handling functions like fopen(), fclose(), fread(), fwrite(), etc.

Test
Q.12 Easy File Handling
What does the third parameter of fseek() represent?
A Number of bytes to move
B The reference point (origin) for seeking
C File handle identifier
D Direction (forward or backward)
Correct Answer:  B. The reference point (origin) for seeking
EXPLANATION

Third parameter is 'whence': SEEK_SET (beginning), SEEK_CUR (current position), SEEK_END (end). Second parameter specifies offset in bytes.

Test
Q.13 Easy File Handling
What is returned by fopen() if file opening fails?
A Returns -1
B Returns NULL pointer
C Returns empty string
D Throws an exception
Correct Answer:  B. Returns NULL pointer
EXPLANATION

fopen() returns NULL pointer on failure. Proper practice is to check: if(fp == NULL) {...error handling...}. Returning -1 is for system calls like open().

Test
Q.14 Easy File Handling
Which function reads formatted data from a file?
A fgets()
B fscanf()
C fread()
D getc()
Correct Answer:  B. fscanf()
EXPLANATION

fscanf() reads formatted data from file (like scanf() but from file). fgets() reads strings. fread() reads binary data. getc() reads single character.

Test
Q.15 Easy File Handling
What will fseek(fp, 0, SEEK_END) do?
A Moves pointer to beginning of file
B Moves pointer to end of file
C Moves pointer 0 bytes forward
D Closes the file
Correct Answer:  B. Moves pointer to end of file
EXPLANATION

SEEK_END moves pointer relative to end of file. Offset 0 means go to the exact end. SEEK_SET (beginning) and SEEK_CUR (current) are other origin options.

Test
Q.16 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.

Test
Q.17 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.

Test
Q.18 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.

Test
Q.19 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.

Test
Q.20 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.

Test
IGET
IGET AI
Online · Exam prep assistant
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