-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileReading.c
62 lines (50 loc) · 1.41 KB
/
FileReading.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
int main(int argc, char*argv[])
{
printf("Please enter file_name.txt and sub string respectively: ");
FILE* file;
file = fopen(argv[1], "r");
if (file == NULL)
{
printf("\n\nError opeing file.\n"); // file not exist error...
}
else // other wise search sub string in file and count them
{
char* y = malloc(30); // dynamic memory
char z; // read from file char by char
int i = 0;
int count = 0; // for counting
while ((z = fgetc(file)) != EOF) // to read the whole file
{
if (z == ' ') // get words from file separated by space
{
y[i] = '\0';
if (strcmp(y, argv[2]) == 0) // cmpare with subString
{
count++; // increment count
}
i = 0;
free(y); // free the dynamic memeory
y = malloc(30); // againg get space for new word
}
else
{
y[i] = z;
i++;
}
}
y[i-1] = '\0';
if (strcmp(y, argv[2]) == 0)
{
count++; // to count the last word from file
}
i = 0;
free(y); // free dynamic memory
printf("\n\nNumber of occurence of sub String is: %d", count);
fclose(file); //close file
}
return 0;
}