3
3
Ans- File handling refers to the process of reading from, writing to, and managing files in a computer
system. It allows programs to create, open, read, write, and delete files on a storage device. Key operations
include:
File handling is essential for data persistence and is commonly used in programming languages like
Python, Java, and C++.
Q.2 Explain the procedure of opening a file with various modes and closing the file with various
functions?
Ans- When working with files in programming, you typically use specific modes to open files, depending on
the intended operation (reading, writing, etc.). Below is an overview of the procedure for opening and
closing files, primarily in Python as an example.
Opening a File - To open a file, you use the built-in open() function with a specified mode. The common
modes are:
1. Read Mode ('r'): Opens a file for reading. If the file does not exist, an error occurs.
python
2. Write Mode ('w'): Opens a file for writing. If the file exists, it is truncated (cleared). If it does not exist, a
new file is created.
3. Append Mode ('a'): Opens a file for appending data. New data is written at the end of the file. If the file
does not exist, a new file is created.
4. Read and Write Mode ('r+'): Opens a file for both reading and writing. The file must exist.
5. Binary Modes: To handle binary files, you can add a b to the mode:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_KEYWORDS 32
#define MAX_KEYWORD_LENGTH 20
// List of C keywords
"default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register",
"return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned",
};
if (strcmp(word, keywords[i]) == 0) {
int main() {
FILE *file;
char filename[100];
char word[100];
int count = 0;
scanf("%s", filename);
if (file == NULL) {
return 1;
}
while (fscanf(file, "%99s", word) == 1) {
if (ispunct(word[i])) {
break; }
} if (is_keyword(word)) {
count++; }
fclose(file);
return 0;
Output: