File Handling Questions in C. This post is for the students of Computer ICS Part 2 | Computer Science 2nd Year Chapter No.14 File Handling in C. Find the most important and more conceptual short questions with their answers on "File Handling in C" for the final examinations of intermediate for all boards of Punjab as well as Pakistan.
We tried our best to provide the exercise programs in simple and easy concepts. This post covers the topics i.e., introduction to file, text stream, binary stream, EOF (end-of-file) marker, fclose() function, fopen() function, getc() function, putc() function, array, file opening modes, and file pointer.
Chapter No.14: File Handling in C
Short Questions & Answers
Q.1: What is a file?
Answer:
A file is a set of related
records. The data is stored on permanent storage in the form of files. In C, a file refers to a disk file, the screen, the keyboard, a port, a
file on tape, and so on.
Q.2: Define the stream?
Answer:
A stream is a logical interface
to a file. A stream is associated with
a file using an open operation. A stream
is a disassociation from a file using a close operation. There are two types of streams i.e. text stream and binary stream.
Q.3: What is a text stream?
Answer:
A text stream is a sequence of
characters. In a text stream, certain
character translation may occur (e.g. a new line may be converted to a carriage
return/line-feed pair). This means that there may not be a one-to-one
relationship between the characters written and those in the external device.
Q.4: What is a binary stream?
A binary stream is a sequence
of bytes with a non-to-one correspondence to those on the external device (i.e.
no translation occurs). The number of bytes written or read is the same as the
number on the external device.
Q.5: What is EOF marker?
Answer:
EOF (End-of-File) is a
code placed by a computer after a file's last byte of data. To mark the end of
a text file, a special end-of-file character is placed after the last character
in the file. Files are stored in blocks, and the end marker helps the computer
know it has allocated enough space to store the file.
Q.6: What is fopen() function?
Answer:
To open a file and associate it with a stream, the fopen() function is used. The fopen() function returns the NULL pointer if
it fails to open the file for some reason. Its prototype is as under:
FILE* fopen(const char* filename,
const char* mode);
Q.7: Define the following file
opening modes:
r, W, A, R+, W+, A+
Answer:
Mode |
Description |
r |
Open a text file
for reading. The file must already exist. |
W |
Open a text file for writing. If the file already exists its contents are overwritten. If it
does not exist, it will be created. |
A |
Open
a text file for append. Data is added to the end of the existing file. If the file does not exist, it is created. |
R+ |
Open a text file for both reading and writing. The file must already exist. |
W+ |
Open a text file for reading and writing and its contents are overwritten. If the file does
not exist, it is created. |
A+ |
Open a text file for both reading and appending. If the file does not exist, it is created for
both reading and writing. |
Q.8: What is the file pointer?
Answer:
A file pointer is a variable of
the type FILE that is defined in stdio.h. A pointer is a memory cell whose content is the address of another
memory cell. To obtain a file pointer variable, a statement like the following
is used:
FILE* fp;
Q.9: What is fclose() function?
Answer:
The fclose() function closes
the file associated with fp, which
must be a valid file pointer previously obtained using fopen(), and disassociates the stream from the file. It also
destroys the structure that was created to store information about file. The fclose() function returns 0 if
successful and EOF if an error
occurs.
The syntax of fclose() is as
follows:
fclose(FILE* fp);
Q.10: What is getc() function?
Answer:
The getc() function reads the
next character from the file and returns it as an integer and if an error occurs
returns EOF. The getc() function also returns EOF
when the end of the file is encountered.
Q.11: What is putc() function?
Answer:
The putc() function writes the character
stored in the variable ch to the file
associated with fp as an unsigned char. Although ch is defined as an int yet we may use a char
instead. The putc() function returns
the character written if successful or EOF
if an error occurs.
Q.12: What is an array?
Answer:
An array is a group of
contiguous memory locations, which can store a large amount of data of the same
datatype.
The general form of an array is as
under:
data_type Array_Name[n];
The data_type specify the type
of data that is stored in every memory location of the array, Array_Name describe the array name, and ‘n’ is the subscript of an array which
shows the total number of locations in the array.
➤ File Handling in C - MCQs
➤ Functions in C - MCQs
➤ Functions in C - Short Questions
➤ Functions in C - Exercise Programs
➤ Loop Constructs - MCQs
➤ Loop Constructs - Exercise Solution
➤ Loop Constructs - Short Questions
➤ Decision Constructs - MCQs
➤ Decision Constructs - Short Questions
➤ Decision Constructs - Exercise Programs
Post a Comment
Your feedback is highly appreciated and will help us to improve. So, please give your good suggestions.