[go: up one dir, main page]

0% found this document useful (0 votes)
33 views3 pages

ITP 2 Assign

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

Performing search operations

on files
PUSHKAR KUMAR (IIT2022213)
AKASH ADAK (IIT2022215)
SANJANA PRAJAPAT (IIT2022214)
RATHLAVATH VINOD (IIT2022212)
PRITAM KUMAR (IIT2022211)

I Semester B.Tech., Information Technology

Abstract— This Paper contains the analysis of Binary files: Binary files are mostly the .bin files in your
a program to find the occurrence of a keyword Computer. Instead of storing data in plain text they store it in
using pointers. The program will have two zeros and ones.
functionalities, of which the first function will
return the no of occurrences of a given word in
the paragraph while the second function willr
return tine no. ,word no., index no. C. Modes of opening a file in C
1- “r” - Searching for a file. When a file is
A. Keywords successfully opened fopen () uploads it to
memory and sets the cursor to the first character
The keywords like fopen, fclose, break used in you are in.
this paper. 2- “w” - Search for a file. If a file exists, its
contents are written again.
I. INTRODUCTION
3- “a” - Searching for a file. When a file is
A. Files successfully opened fopen () uploads it to memory
and sets the cursor to the last character in it.
A file represents a sequence of bytes on the disk 4- “r +” - Searching for a file. When successfully
where a group of related data is stored. File is opened fopen () loads it into memory and sets the
created for permanent storage of data. cursor to the first character in it.
5- “w +” - Searching for a file. If a file exists, its
contents are written down. If the file does not exist
B. Types of files a new file is created.
1) Text files: Text files are normal .txt files. You 6- “a +” - Searching for a file. When a file is
can easily create text files using any simple text successfully opened fopen () uploads it to memory
editors such as Notepad. and sets the cursor to the last character in it.
II. APPROACH         scanf("%s", str);
1) The main function starts with the definition of an         int line = 1;
integer variable t with the initial value of 1,
        int num = 0;
which will be used as a control variable for the
while loop that follows. The loop continues as
long as it is not equal to 0.         while (!feof(ptr))
2) The first step inside the loop is to open the file         {
"myfile.txt" using the fopen function and assign             fgets(s, 400, ptr);
the file pointer to a variable ptr. If the file doesn't
            int count = 0, word = 1, x = 0;
exist, the function returns a null pointer.
            for (int i = 0; s[i] != '\n'; i++)
3) The program then asks the user to enter a word
they want to search for and stores it in a string             {
variable str. The line, word, and index numbers of                 for (int j = 0; j <
all occurrences of the word in the file are then strlen(str); j++)
displayed.
                {
                    if (s[i + j] == str[j])
4) The program uses two nested for loops to search
for the word in the file. The outer loop reads each                         count++;
line of the file using the fgets function, and the                     else
inner loop compares the characters of the line                     {
with the characters of the searched word. If the
                        count = 0;
characters match, a counter-variable count is
incremented. If the count is equal to the length of                         break;
the searched word, it means that the word has                     }
been found, and its line, word, and index numbers                 }
are displayed.

                if (count == strlen(str))
5) The program continues to read the file line by
line until it reaches the end of the file, which is                 {
determined by the feof function. The fclose                     printf("word found at line
function is then used to close the file. number %d word number %d and index number %d \
6) The program asks the user if they want to n", line, word, i - x);
continue searching for words in the file by
                    num++;
entering 0 to exit or 1 to continue. If the user
enters 0, the loop ends, and the                 }
program terminates                 if (s[i] == ' ')
                {
III . APPENDIX                     x = i;
                    word++;
                }
Link to original code: Performing_search_operations_on _files.c
#include <stdio.h>             }
#include <stdlib.h>             line++;
#include <string.h>         }
int main()         printf("word found %d times in the
{ paragraph \n", num);
    int t = 1;         fclose(ptr);
    while (t)         printf("enter 0 to exit\nenter 1 to
    { continue\n");
        FILE *ptr = NULL;         scanf("%d", &t);
        ptr = fopen("myfile.txt", "r");     }
        char s[100000];     return 0;
        char str[30]; }
        printf("enter the word you want
to find :\n");
IV. ACKNOWLEDGMENT
We would like to express our special thanks to our
computer programming teacher Mr. Mohammed Javed sir and
his teaching assistant Mr. Ashutosh Gupta sir for their
guidance and support in completing our project.
V. REFERENCES
7) Tutorialspoint
8) Programming in ANSI C - E. Balaguruswamy(6th Edition)
9) Geeks For Geeks

You might also like