Q: 1 The purpose of rewind () function in C is ____________.
file pointer is initialized to beginning of the file
file is closed and freshly opened
file pointer is initialized to end of file
file pointer is set to null
[ Option A ]
In C file handling, every open file is associated with a file pointer that indicates the current position for reading or writing within the file. As operations like fread(), fwrite(), and fgets() are performed, this pointer automatically moves forward, tracking the progress through the file.
The rewind() function is used to reset this file pointer back to the beginning of the file (byte 0). It allows the program to read or process the file again from the start without closing and reopening it, making file operations more efficient and convenient.
The rewind() function also clears any error or EOF (End Of File) flags associated with the file.
| Function | Used For |
|---|---|
| rewind() | Moves pointer to beginning. |
| fseek() | Moves pointer to a specific position. |
| ftell() | Returns current position. |
Q: 2 In the given C code fragment, fill xxxx with an appropriate C keyword / function to find the end of file.
FILE *infile = fopen(fn,”r”);
len = ftell(infile);
if(!xxxx)
{
printf(“End of file is not reached”);
}
fseek(infile)
eof(infile)
feof(infile)
EOF
[ Option C ]
In C, the feof() function is used to check whether the End of File (EOF) has been reached for a file stream. It returns a non-zero value if EOF is reached, otherwise it returns zero.
The condition if(!feof(infile)) checks whether the end of the file has not been reached. The function feof(infile) returns a non-zero value when the end of file is reached, so using the logical NOT operator (!) means the condition becomes true when the file has not yet reached its end, and therefore the message is printed.
Q: 3 Each file can be accessed through a file mode. These file modes decide what action can be performed in the file. If the file mode is <r> then we can only _________ the contents of the file.
Read
Write
Delete
Overwrite
[ Option A ]
File modes control the type of access you have to a file. When a file is opened in the mode <r>, it means the file is opened for reading only. In this mode, you can only view or read the contents of the file, but you cannot make any changes like writing, deleting, or overwriting the file.
| FILE MODE | OPERATIONS ALLOWED | DESCRIPTION |
|---|---|---|
| r | Read | Open for reading only. File must exist, otherwise, error occurred. |
| w | Write | Open for writing only. Overwrites existing file or creates new file if it does not exist. |
| a | Append (Write) | Open for appending. Data written will be added to the end. Creates file if it does not exist. |
| r+ | Read and Write | Open for both reading and writing. File must exist, otherwise, error occurred. |
| w+ | Read and Write | Open for reading and writing. Overwrites or creates new. |
| a+ | Read and Write | Open for reading and appending. Creates file if it does not exist. |
| rb | Read Binary Data | Open for reading in binary mode. File must exist. |
| wb | Write Binary Data | Open for writing in binary mode. Overwrites or creates new. |
| ab | Append (Write binary data) | Open for appending in binary mode. Creates file if doesn't exist. |
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.