0% found this document useful (0 votes)
8 views

3

notes

Uploaded by

ayushsonid078
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

3

notes

Uploaded by

ayushsonid078
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PRE - LAB 3

Q.1 What is File Handling?

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:

1. Opening Files: Accessing a file to perform operations.

2. Reading Files: Retrieving data from a file.

3. Writing Files: Storing data in a file.

4. Closing Files: Releasing resources associated with a file.

5. Error Handling: Managing issues that arise during file operations.

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

file = open('filename.txt', 'r')

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.

Python-- file = open('filename.txt', 'w')

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.

Python -- file = open('filename.txt', 'a')

4. Read and Write Mode ('r+'): Opens a file for both reading and writing. The file must exist.

Python -- file = open('filename.txt', 'r+')

5. Binary Modes: To handle binary files, you can add a b to the mode:

- 'rb': Read binary

- 'wb': Write binary

- 'ab': Append binary


Experiment-03:
Write a program in C to count total no of keywords in a file ? (Text File)

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#define MAX_KEYWORDS 32

#define MAX_KEYWORD_LENGTH 20

// List of C keywords

const char *keywords[MAX_KEYWORDS] = { "auto", "break", "case", "char", "const", "continue",

"default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register",

"return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned",

"void", "volatile", "while"

};

int is_keyword(const char *word) {

for (int i = 0; i < MAX_KEYWORDS; i++) {

if (strcmp(word, keywords[i]) == 0) {

return 1; // Found a keyword

return 0; // Not a keyword

int main() {

FILE *file;

char filename[100];

char word[100];

int count = 0;

printf("Enter the filename: ");

scanf("%s", filename);

file = fopen(filename, "r");

if (file == NULL) {

printf("Error opening file!\n");

return 1;

}
while (fscanf(file, "%99s", word) == 1) {

// Remove any punctuation from the word (if needed)

for (int i = 0; word[i]; i++) {

if (ispunct(word[i])) {

word[i] = '\0'; // Null-terminate at the first punctuation

break; }

} if (is_keyword(word)) {

count++; }

fclose(file);

printf("Total number of keywords: %d\n", count);

return 0;

Output:

Enter the filename: example.c

Total number of keywords in the file: 10

You might also like