File.txt
Jerry Owen
Start time was 1:45PM
bla bla bla
bla bla bla
bla bla bla
Byron Owen
Start time was 2:45PM
bla bla bla
bla bla bla
bla bla bla
bla bla bla
bla bla bla
Kathy Owen
Start time was 3:45PM
bla bla bla
Im trying to get all lines with the word time in it. How do I make this work? The only thing it does right now is to find the word time, but only one, and fgets does nothing with the line for me.
grep works well:
grep time file.txt
but I need to do everything in C.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 15
#define FIND "time"
char fileText[14];
int main() {
FILE *fptr, *fptw;
fptr=fopen("file.txt","r");
fseek(fptr, 0, SEEK_END);
long pos = ftell(fptr);
fseek(fptr, 0, SEEK_SET);
char *arr = malloc(pos + 1);
fread(arr, pos, 1, fptr);
arr[pos] = '\0';
if (strstr(arr, FIND) != 0){
fgets(fileText, MAX, fptr);
printf("found\n");
printf("%s\n\n",fileText); }
else{
printf("Not found\n"); }
free(arr);
return 0;
}