forked from ashish7802/cpp-cli-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_ops.c
More file actions
36 lines (34 loc) · 879 Bytes
/
Copy pathfile_ops.c
File metadata and controls
36 lines (34 loc) · 879 Bytes
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
#include "file_ops.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* read_file(const char* filename) {
FILE* file = fopen(filename, "r");
if (!file) return NULL;
fseek(file, 0, SEEK_END);
long length = ftell(file);
fseek(file, 0, SEEK_SET);
char* buffer = malloc(length + 1);
if (!buffer) {
fclose(file);
return NULL;
}
fread(buffer, 1, length, file);
buffer[length] = '\0';
fclose(file);
return buffer;
}
int search_word(const char* filename, const char* word) {
FILE* file = fopen(filename, "r");
if (!file) return 0;
char line[256];
int found = 0;
while (fgets(line, sizeof(line), file)) {
if (strstr(line, word)) {
found = 1;
break;
}
}
fclose(file);
return found;
}