-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_search.c
34 lines (34 loc) · 910 Bytes
/
linear_search.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
#include <stdio.h>
int main() {
FILE *file;
char filename[100];
int search_key, num;
int found = 0;
int index = 0;
printf("Enter the filename to open: ");
scanf("%s", filename);
printf("Enter the integer to search for: ");
scanf("%d", &search_key);
file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s\n", filename);
return 1;
}
for (index = 0; fscanf(file, "%d", &num) != EOF; index++) {
if (num == search_key) {
printf("Found the key %d in the file at index %d\n", search_key, index);
found = 1;
break;
}
}
if (!found) {
printf("%d was not found in the file.\n", search_key);
}
fclose(file);
return 0;
}
/*Sample Output
nter the filename to open: i3.txt
Enter the integer to search for: 5
Found the key 5 in the file at index 404
*/