Searching a text file

im have real trouble with this one, im trying to make a function that will take in a users ID and search a text file for that ID and if there is a match to print out the matched line and the following 3 lines that follow. so far i ahve the following code but i cant get that to work and i dont knwo how to do the rest. Can anyone help?
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
//declaring a file variable
    FILE *fpcust;   
    int line_num = 1;
    int find_result = 0;
    char temp[512];
    int str;
    char ch;
    
    
    //opening file for reading
    fpcust = fopen ("c:\\customers.txt", "r");   
    
    
    //error check
    if (fpcust == NULL) 
    {
        printf("Cannot open file. \n");
    }
    
    
    while (fgets(temp, 512, fpcust) != NULL) 
    {
        if ((strstr ( temp, str ) ) != NULL )
        {
            printf("Match found on line: %d", line_num);
            printf("\n%s\n", temp);
            find_result++;
        }
        line_num++;
    }
    
    
    if (find_result == 0)
    {
        printf("Sorry there are no matches. \n");
    }
    
    
    //closing the file
    fclose(fpcust);
}    
http://www.cplusplus.com/reference/string/string/find/

off on a tangent answer:

If you are storing a struct in a file,

1
2
3
4
5
struct employee{
  int ID;
  char name[25];
  .....
};


something like that, and each ID is unique, then you can read and write it as binary and seek by ID*structsize

This is how direct access files work.
im not too sure how i will relate that to my code, i havnt had a look at the link tho
is the std::string c++ or something different?
Topic archived. No new replies allowed.