After reading from text file, search results aren't correct

I'm trying to write a program segment that allows the user to search through a database of profiles (which are stored inside a text file). The program accepts an ID from the user and attempts to find a profile with a matching ID in the database. It will then display the matching profile.

As I've tried to code this, I've encountered an issue:

Contents of the text file:
1
2
M0001 Cool Name F 123456789
M0002 Name Cool M 987654321


When I search for the ID M0001, the search result is:
 
M0001 Cool Name F 123456789 M 0002


And when I search for the ID M0002, I get:
 
No matching records found.


How do I make it so it displays the results correctly?

Here's the program segment I'm having trouble with (forgive me if there any inefficiencies or whatnot with the way I code; my knowledge of programming is currently rather limited):
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
printf("ID to search > ");
rewind(stdin);
scanf("%s", idSearch);

memberSearch = fopen("member.txt", "r");
while (fscanf(memberSearch, "%s %[^\n] %c %s\n", search.id, search.name, &search.gender, search.contact) != EOF) {
   if (stricmp(idSearch, search.id) == 0) {
        strcpy(seek.id[c], search.id);
        strcpy(seek.name[c], search.name);
        seek.gender[c] = search.gender;
        strcpy(seek.contact[c], search.contact);
        c++;
    }
}
fclose(memberSearch);

if (c == 0) {
    printf("\n\nNo matching records found.\n\n");
else {
    system("cls");
    printf("ID\tName\t\tGender\tContact\n");
    printf("==\t====\t\t======\t=======\n");
    for (a = 0; a < c; a++)
        printf("%s %s %c %s\n", seek.id[a], seek.name[a], seek.gender[a], seek.contact[a]); // I haven't formatted the spacing yet.
    
    printf("\nMatching searches found: %d\n\n", a);

    c = 0;
}


Just to verify, I have made sure I included:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char id[6], name[100], gender, contact[12];
} Member;
Member search;

typedef struct {
    char id[1000][6], name[1000][100], gender[1000], contact[1000][100];
} Search;
Search seek;


And inside the main program:
1
2
3
FILE *memberSearch;
int a, c = 0, e = 0;
char idSearch[6];


Help would be appreciated! And also ways in which I can improve the way I code.
Last edited on
It looks like your fscanf() is incorrect, in several places.

fscanf(memberSearch, "%s %[^\n] %c %s\n", search.id, search.name, &search.gender, search.contact)

First you're risking buffer overflows when retrieving your strings, you should always use the correct width specifier when retrieving strings.

Second the "%[^\n]" specifier only stops when it encounters the newline character, so it will try to retrieve everything up to the newline character into the supplied variable. If you look at each variable you should find that your have something like:

search_id: "M0001"
search.name: "Cool Name F 123456789"
search.gender: 'M'
search.contact: "0002"

The "%[^\n]" specifier grabs everything until the the end of line, you only want to retrieve the member's name, so you'll need to parse the file differently.

Third your Search structure should probably not needed, just use an array of your "Member" structure (and the instances of the structures should not be global).


1
2
3
4
5
6
7
8
9
10
11
typedef struct {
    char id[6], name[100], gender, contact[12];
} Member;

int main(void)
{
   Member search;
   Member members[1000];

...


Fourth you may want to consider reading the complete file into your array of Member variable and then search that array for the desired members.

Also be aware that stricmp() is not a standard C function and may not be available with every compiler.


Topic archived. No new replies allowed.