Searching

ok i have made the following to search a text file for a user input, the code works fine if the item is on the first line of the text file but not if its on any other line, can someone help me fix this so it scans every line instead of just the first one?

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


//delcaring the function prototypes
void search (void);

main()
{
    
    search();
    flushall();
    
    getchar();
    flushall();
    

    return 0;
    
}//end to main
  

//function search
void search (void)
{
    FILE *fpcust;
    int i, len_string;
    char searchID [30];
    char temp[30];
    
    printf("What do you want to search for? \n");
    scanf(" %s", searchID);
    
    fpcust = fopen ("c:\\presdatatest.txt", "r");
    rewind(fpcust);
    
    if ( fpcust == NULL)
    {
        printf("File cound not be opened.");
        exit(0);
    }
    
    else
    {
        len_string = strlen(searchID);
        
        while (!feof ( fpcust ) )
        {
            for (i = 0; i < len_string; i++)
            {
                temp[i] = fgetc ( fpcust );
            }
            temp[i] = '\0';
            
            //strcmp used for comparing both strings
            if ( strcmp ( searchID, temp ) == 0)
            {
                printf("The ID was found \n");
                fclose( fpcust);
                system ("PAUSE");
                exit(1);
            }
            
            else
            {
                printf("No matches found. \n");
                system ("PAUSE");
                exit (1);
            }
            
            fseek ( fpcust, - (len_string - 1), 1);
        }
        fclose ( fpcust );
    }    
}
  
I'm going to have to look at this later, but I also noticed that your program will not find a string unless it is in the first char of the string.

Such as I have a file with string "345"
Your program will find 34, but not 45...

i have the code working thankfully, but do you know how i would go about printing the contents of the line i have found and the following 6 lines? this is the code as it stands
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

//delcaring the function prototypes
void search (void);

char* fixFgetsStr( char* s )
{

    char* p = strchr( s, '\n' );
    if( p )
        *p = 0; /* strip off '\n' char */
    else
        while( fgetc( stdin ) != '\n' ) ; /* flush rest of stdin line */
            
    return s;
}

main()
{
    
    search();
    flushall();
    
    getchar();
    flushall();
    

    return 0;
    
}//end to main
  

//function search
void search (void)
{
    FILE *fpcust;
    int i, len_string;
    char buf [512];
    char searchID [30];
   
 

    fpcust = fopen ("c:\\presdatatest.txt", "r");
    rewind(fpcust);
    
    if ( fpcust == NULL)
    {
        printf("File cound not be opened.");
        exit(0);
    }
    
    else
    {
        len_string = strlen(searchID);
        
        while ( 1 )
        {
            int line_num = 0;
            int find_result = 0;
            
            printf("Please enter your ID: \n");
            fflush ( stdout);
            scanf( "%20s", searchID );
            while ( getchar() != '\n' );
                
            while ( fgets ( buf, 512, fpcust ) )
            {
                line_num ++;
                fixFgetsStr ( buf );
                
                if (strstr ( buf, searchID ) )
                {
                    printf("Match found");
                    find_result++;
                }
                
            }
            if ( !find_result )
            {
                printf("No matches found...");
            }
            
            fputs ( "\n \n Do you have any more searches? (Y/N) ", stdout );
            fflush ( stdout );
            fgets ( buf, 512, stdin );
            
            if ( toupper ( buf[0] ) == 'n' ) break;
            rewind ( fpcust );
        }
        
    }
        fclose ( fpcust );
}    
Not knowing what your input file looks like it's hard to guess.

I would write a iffound function, and have it read/print the next 6 lines.
or
If the structure is the same, meaning there is 7 lines per ID, you could read in 7 lines at a time, see if ID is in line 1, if match found, then print the 7 lines.

sorry i thought i copied a segment in, this is what it looks like. i am searching for the ID and want to print out the ID line and the following 5 lines after that.

4567
Mike
Green
52
m
grafton street 5
4568
Svetlana
Reed
41
f
baker street 24
Topic archived. No new replies allowed.