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 The fseek() is a better function than rewind() because
The rewind() cannot be used for empty files
The fseek() return the status of operation, rewind() does not
The fseek() gets all the information about file, including size
The rewind() can be used for many file types
[ Option B ]
In C file handling, both fseek() and rewind() functions are used to move the file pointer, but they differ in flexibility and feedback.
The fseek() allows moving the file pointer to any desired position and also returns a value or status indicating whether the operation was successful or not. It returns 0 on success.
The rewind() simply moves the file pointer to the beginning of the file and does not return any value, so it provides no status about success or failure.
Q: 4 The 1st and 2nd arguments of fscanf() are
Identifier(s) string, Filename in char array
Filename in char array, variable list separated by ‘,’
File pointer, identifier(s) string
Filename in char array, File pointer
[ Option C ]
In C language, fscanf() is used to read formatted input from a file. It works similarly to scanf(), but instead of reading from standard input i.e., keyboard, it reads from a file.
Syntax:
fscanf(FILE *fp, const char *format, variable_list);
The first argument is a file pointer (FILE *fp), which specifies from which file data should be read.
The second argument is the format string, which defines the type and format of data to be read like %c, %d, %f, %s, etc.
After that, variables are passed where the read values will be stored.
#include <stdio.h>
int main()
{
FILE *fp;
int id;
char name[20];
fp = fopen("myfile.txt", "r");
fscanf(fp, "%d %s", &id, name);
printf("Rollno : %d, Name : %s", id, name);
fclose(fp);
return 0;
}
Q: 5 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);
int c=getc(infile);
if(c!=xxxx)
printf(“End of file is not reached”);
fseek(infile)
eof(infile)
feof(infile)
EOF
[ Option D ]
In C, getc() reads one character from a file and returns the character value if a character was read, or the special macro EOF if the end of file is reached or an error occurs.
So, to check whether the end of the file has been reached when using getc(), we compare its return value with EOF, not with any function call.
In the given code int c = getc(infile); reads a character, and the condition if(c != xxxx) should check whether the character is not equal to EOF, meaning the file has not ended yet.
Q: 6 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.