Home Subjects C Programming File Handling

C Programming
File Handling

C language from basics to advanced placement prep

52 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 41–50 of 52
Topics in C Programming
Q.41 Medium File Handling
Which function is used to read a string from a file, including whitespace characters, up to a maximum length?
A fscanf()
B fgets()
C getc()
D fread()
Correct Answer:  B. fgets()
EXPLANATION

fgets() reads a string from file including whitespace until newline or EOF is encountered or max length is reached.

Test
Q.42 Medium File Handling
What happens when fseek() is used with SEEK_END as origin?
A File pointer moves to end of file
B File pointer stays at current position
C File pointer moves to beginning
D File is closed
Correct Answer:  A. File pointer moves to end of file
EXPLANATION

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.

Test
Q.43 Medium File Handling
Consider the code: FILE *fp = fopen("data.txt", "r"); if(fp == NULL) { printf("Error"); } What does this check?
A Whether file opened successfully
B Whether file is empty
C Whether file pointer is valid
D Whether file has read permission
Correct Answer:  A. Whether file opened successfully
EXPLANATION

This code checks if fopen() returned NULL, which indicates the file failed to open. This is the standard error checking method for file operations.

Test
Q.44 Medium File Handling
What is the purpose of rewind() function in file handling?
A Moves file pointer to the beginning
B Closes the file
C Reverses file content
D Undoes last write operation
Correct Answer:  A. Moves file pointer to the beginning
EXPLANATION

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).

Test
Q.45 Medium File Handling
What does fwrite() function return?
A Number of items successfully written
B Number of bytes written
C 1 on success, 0 on failure
D Always returns 0
Correct Answer:  A. Number of items successfully written
EXPLANATION

fwrite() returns the number of items successfully written to the file, which may be less than the count requested if an error occurs.

Test
Q.46 Medium File Handling
Which mode allows both reading and writing without truncating?
A "r+"
B "w+"
C "a+"
D "rb+"
Correct Answer:  A. "r+"
EXPLANATION

"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.

Test
Q.47 Medium File Handling
What is the difference between fgetc() and getc()?
A fgetc() is a function while getc() is a macro
B getc() is faster than fgetc()
C fgetc() can only read one character
D getc() requires file pointer argument
Correct Answer:  A. fgetc() is a function while getc() is a macro
EXPLANATION

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.

Test
Q.48 Medium File Handling
What does feof() function check?
A Whether end of file is reached
B Whether file is empty
C Whether file exists
D Whether file is open
Correct Answer:  A. Whether end of file is reached
EXPLANATION

feof() returns true (non-zero) if the end-of-file indicator is set for the given file stream, otherwise it returns 0.

Test
Q.49 Medium File Handling
Which function sets the file position to a specific location?
A fseek()
B setpos()
C fsetpos()
D seek()
Correct Answer:  A. fseek()
EXPLANATION

fseek() is used to move the file pointer to a specific position in the file. It takes three arguments: file pointer, offset, and origin.

Test
Q.50 Medium File Handling
What is the purpose of ftell() function?
A Returns current file position
B Tells if file exists
C Tells file name
D Tells file size
Correct Answer:  A. Returns current file position
EXPLANATION

ftell() returns the current position of the file pointer in bytes from the beginning of the file.

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