0

Sorry if the title is confusing. It might be better if I explain with the below code blocks.

Part of my code

typedef struct {
    char name[50];
    char num[9];
    char email[50];
} Contacts;
Contacts Contact[50];

void search() {
    char string[20];
    printf("What are you searching for: ");
    scanf("%19s", string);
    file = fopen("StoredContacts.txt", "r");
    while (!feof(file)) {
        
    }
}

StoredContacts.txt

Bobby,10,bobby@gmail.com
Abby,30,abby@gmail.com
Ada,20,ada@gmail.com

So basically my StoredContacts.txt contains names, ages and emails. I separated the contacts with a comma and they can be correctly registered into Contact.

What I want my search() to do is when I entered what I was looking for, the function would check each Contact and return me the full details of the person(s) I was looking for. For example, if I entered "A", I want it to print "Abby,30,abby@gmail.com" and "Ada,20,ada@gmail.com". If I entered "da", I want it to return "Ada,20,ada@gmail.com". I tried using bsearch and realised it couldn't print the details of the person(s).

Mochi
  • 1
  • 2
  • `char string` is a *single character*, and you can't `scanf("%s")` into that. As C strings are NUL terminated, that's effectively a zero-length C string buffer. – tadman Dec 17 '22 at 18:22
  • fix the reading as per other comment, then use strstr – pm100 Dec 17 '22 at 18:24
  • ...Ignoring that, you do not use the ```&``` operator with ```%s``` . – Harith Dec 17 '22 at 18:24
  • @tadman Sorry, I am new to coding and I am doing this for school project. Will making char string[20] fix the problem? Thanks. – Mochi Dec 17 '22 at 18:25
  • @Mochi It will, if you remove the ```&``` from the second argument, as the name of the array is a pointer to the first element of that array, which satisfies ```scanf```. But that gives birth to another problem, what happens when you enter more than 20 characters? You don't specify any bounds, so it's vulnerable to a buffer overflow. – Harith Dec 17 '22 at 18:26
  • Aside: ```scanf``` returns something, check for it. – Harith Dec 17 '22 at 18:29
  • 1
    to read whole line just use fgets – pm100 Dec 17 '22 at 18:30
  • @Haris Buffer overflow won't cause any problems for now since it is just a school project, it won't be used by the general public. The aim is just to make functions that can do certain things. – Mochi Dec 17 '22 at 18:31
  • Tip: Pick buffers that are fairly generous, like `char s[255]` as an example, then use that in your scan call, as in `scanf("%254s", &s)` in this case, leaving 1 character for the NUL byte. **ALWAYS** check the results of calls like `scanf` to be sure they have succeeded before using any values. You may have had a failed call, and that string might be corrupted or undefined. There's lots of advice on here on [avoiding problems like that](https://stackoverflow.com/questions/1621394/how-to-prevent-scanf-causing-a-buffer-overflow-in-c). – tadman Dec 17 '22 at 18:40
  • @tadman got it. But still, I don't understand how do I search through the file for words. – Mochi Dec 17 '22 at 18:43
  • 2
    obligatory [Why is “while( !feof(file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) link. – n. m. could be an AI Dec 17 '22 at 19:20
  • @Mochi `scanf("%254s", &s)` should generate a warning. Surely you meant `scanf("%254s", s)`. – chux - Reinstate Monica Dec 17 '22 at 19:42

1 Answers1

0

I used strstr and finally made the search function.

The function now at first tests if there are any results that contain the string, then it runs again and prints the details of the person that contains the string in their name or age, or email.

void search() {
int result_count = 0;
char string[20];
char *result1, *result2, *result3;
printf("What are you searching for: ");
scanf("%19s", string);
file = fopen("StoredContacts.txt", "r");
if (!feof(file)) {
    for (int i = 0; i < stored; i++) {
        result1 = strstr(Contact[i].name,string);
        result2 = strstr(Contact[i].num,string);
        result3 = strstr(Contact[i].email,string);
        if (result1 || result2 || result3) {
            result_count++;
        }
    }
}
if (result_count > 0) {
    printf("There are %d results:\n", result_count);
    if (!feof(file)) {
        for (int i = 0; i < stored; i++) {
            result1 = strstr(Contact[i].name,string);
            result2 = strstr(Contact[i].num,string);
            result3 = strstr(Contact[i].email,string);
            if (result1 || result2 || result3) {
                printf("%s,%s,%s\n", Contact[i].name, Contact[i].num, Contact[i].email);
            }
        }
    }
}
else if (result_count <= 0) {
    printf("None of the contacts contains %s", string);
} 
fclose(file);
menu();
Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
Mochi
  • 1
  • 2