Showing 91–100 of 100 questions
in 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.
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.
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.
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.
Which function writes formatted data to a file?
A
fprintf()
B
fwrite()
C
fputs()
D
fputc()
Correct Answer:
A. fprintf()
EXPLANATION
fprintf() writes formatted data to a file, similar to printf() but with a file pointer as the first argument.
What does fgets() function do?
A
Reads a line from a file
B
Gets file status
C
Gets file size
D
Gets file pointer position
Correct Answer:
A. Reads a line from a file
EXPLANATION
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.
Which mode opens a file for writing and truncates it if it exists?
A
"w"
B
"a"
C
"r+"
D
"w+"
EXPLANATION
"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.
Which function is used to close a file in C?
A
fclose()
B
close_file()
C
file_close()
D
closefile()
Correct Answer:
A. fclose()
EXPLANATION
fclose() is the standard function to close an opened file stream. It returns 0 on success and EOF on error.
What does the mode 'rb' mean in fopen()?
A
Read in binary mode
B
Read and backup mode
C
Rewrite and binary mode
D
Remove and backup mode
Correct Answer:
A. Read in binary mode
EXPLANATION
'rb' mode opens a file for reading in binary format. Binary mode reads the exact bytes without any text conversion.
Which function is used to open a file in C?
A
fopen()
B
open_file()
C
file_open()
D
openfile()
Correct Answer:
A. fopen()
EXPLANATION
fopen() is the standard C library function used to open a file. It takes two arguments: filename and mode.