2nd Year Computer Chapter 14 File Handling in C Question And Answer
Short And Simple Question And Answer
Q 1. What is a data file?
Ans. A data file is a collection of related records, with each record comprising multiple fields. Data files store various types of data permanently.
Q 2. What is a stream?
Ans. A stream refers to the flow of data from one point (source) to another point (destination). The source sends data, and the destination receives it.
Q 3. What are the different types of streams?
Ans. There are various types of streams used for data transfer, including:
- Input stream
- Output stream
- Binary stream
- Text stream
Q 4. What is a text stream?
Ans. A text stream involves the flow of characters from a source to a destination. In a text stream, characters are converted into bytes, and there may not be a one-to-one correspondence between characters and bytes due to factors like line breaks.
Q 5. What is a binary stream?
Ans. A binary stream consists of the flow of bytes from a source to a destination. Unlike text streams, no translation is required in binary streams, and there is a one-to-one correspondence between the bytes read or written and those on the external device.
Q 6. What is an input stream?
Ans. An input stream refers to the flow of data from a source to a program. For instance, in C language, when data is read from a data file, it is considered an input stream.
Q 7. What is an output stream?
Ans. An output stream is the flow of data from a program to a destination. In C language, when data is written to a data file, it is part of an output stream.
Q 8. What is a pointer?
Ans. A pointer is a special variable used to store memory addresses, rather than data. Pointers come in different types, and the type of pointer should match the type of variable whose address it holds.
Q 9. What is a file pointer?
Ans. A file pointer is a variable of type File, a special data type defined in the stdio.h header file. It contains information about an opened file and is used to read and write in a data file.
Q 10. What is meant by EOF?
Ans. EOF, denoting “end of file,” is a marker used at the end of text files to indicate the file’s termination. It is placed after the last character of a text file and is used to detect the end of the file.
Q 11. What is a string?
Ans. A string is a collection of characters enclosed in double quotations. In C language, there is no special data type to store strings, so a char array is used to hold strings.
Q 12. What is the purpose of the fopen() function?
Ans. The fopen() function is used to open a file before reading from or writing to it. It transfers the file’s data from secondary storage to main memory, and a file pointer is attached to the open file.
Q 13. What is the purpose of the fputs() function?
Ans. The fputs() function is employed to write a string to a text file. It must be used after opening the file in write or append mode and follows the syntax: “fputs(string, file pointer).”
Q 14. What is the purpose of the fgets() function?
Ans. The fgets() function is used to read a string from a text file. It should be used after opening the file in read mode and has the syntax: “fgets(str, n, file pointer).”
Q 15. What is the purpose of the putc() function?
Ans. The putc() function is used to write a single character into a text file one at a time. It requires the file to be opened in write or append mode and follows the syntax: “putc(character, file pointer).”
Q 16. What is the purpose of the getc() function?
Ans. The getc() function is employed to read a single character from a file. Multiple characters can be read from the file by using this function repeatedly. It should be used after opening the file in read mode.
Q 17. What is the purpose of the fprintf() function?
Ans. The fprintf() function is used to write data in a specified format to a file. It can write any type of data to a file, which must be opened in write or append mode. The general syntax is: “fprintf(fp, Format_string, argument).”
Q 18. What is the purpose of the fscanf() function?
Ans. The fscanf() function is utilized to read data from a file in a specific format. It works similarly to the scanf() function but reads data from a file rather than from the keyboard. Its general syntax is: “fscanf(fp, Control-string, Var).”
Q 19. What is the purpose of the fclose() function?
Ans. The fclose() function is used to close a file that has been opened by the fopen() function. When a file is closed, its contents are transferred from main memory to secondary storage, and the connection between the C program and the file is terminated. This function ensures that data is properly saved and not lost.