File I/O (Input/Output) in C refers to the process of reading from and writing to files on the disk. File I/O in C is done using the standard library functions provided in stdio.h. These functions allow you to open files, read from or write to them, and close them when done.
Types of File I/O operations
1. Opening a file
2. Reading from a file
3. Writing to a file
4. Closing a file
Open a File
To open a file in C, you use the fopen() function. It requires two arguments:
1. The name of the file.
2. The mode in which you want to open the file.
Syntax:
FILE *fopen(const char *filename, const char *mode);
Explanation:
- filename is the name of the file to be opened.
- mode specifies the file access mode (whether to read, write, append, etc.).
There are many modes of File
C File I/O (Input/Output) – Interview Questions
Q 1: What is File I/O in C?
Ans: Reading and writing data to files.
Q 2: Which header file is used for file handling?
Ans: <stdio.h>.
Q 3: What is FILE?
Ans: A structure that holds file information.
Q 4: What function is used to open a file?
Ans: fopen().
Q 5: What are file modes in C?
Ans: r, w, a, r+, w+, a+.
C File I/O (Input/Output) – Objective Questions (MCQs)
Q1. Which header file is required for file handling functions in C?
Q2. What is the data type of a file pointer in C?
Q3. Which function is used to open a file in C?
Q4. What will fclose() do in C?
Q5. Which of the following is a valid file opening mode in C?