Search code examples
cstrncmp

How to compare user input string with string in a file in C


So Im trying to make a program that searches in a file for a word that the user inputs. If theres a match, it say "FOUND IT", if it couldnt find anything, it says " COULDNT FIND ANYTHING" (obviously :p). Im not clear at how to scan the file for the word that the user chooses (writes through scanf).

Here's my (non-functional) code. Thanks for any advice !

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string.h>
#include <string>
#define slovo 255

using namespace std;
 int _tmain(int argc, _TCHAR* argv[])
{
int i;
char secret[slovo];
int outexists,fileexists;
system("Color 02");

setlocale(LC_ALL, "");
FILE*out = fopen("additions.txt", "w+");
if (!out)
{
    perror("additions.txt");
    getchar();
    return 1;

}
else
{
    outexists = 1;
}
FILE*file = fopen("db.txt", "r");

if (!file)
{

    perror("db.txt");
    getchar();
    return 1;

}
else
{
    fileexists = 1;
}
char artist[slovo];

printf("Welcome, hit ENTER to start looking.\n");
getchar();
printf("Please enter for the word you wish to search for in our database !
\n    ");

scanf("%s",&artist);



**/* THIS PART */** if (fileexists == 1 && outexists == 1)
    {
        while (fscanf(file, "%c", secret) != EOF){

            { if (!strncmp(secret, artist, sizeof(artist)))
            {
                printf("FOUND IT \n");
                fclose(file);
            }
            else
            {
                printf("COULDNT FIND IT \n");
                fclose(file);

            }
            }

        }


}







printf("Which word do you wish to add  ?\n");
scanf("%s", artist);
fprintf(out, "%s", artist);
printf("done! \n");
getchar();




fclose(file);
fclose(out);


getchar();



return 0;

}


Solution

  • You probably need the conversion pattern %s instead of %c, the latter read only one char. the former reads a string.

    fscanf(file, "%s", secret)
    

    EDIT

    just saw that too,

    fclose(file);
    

    you close the file in both the if and the else inside the while loop after only one fscanf + strncmp. do not do that, close the file after the wile loop has terminated.

    Also simply use strcmp instead of strncmp.

    EDIT 2

    So, after copy pasting you 'tricky' code which btw, turned my console font color into green, I was able to find a word in a text file:

    First get rid of that getchar();!

    printf("Welcome, hit ENTER to start looking.\n");
    getchar();
    

    I was always typing the key to search for after the Welcome, hit ENTER to start looking. is printed and when hitting enter, nothing is read because getchar() was waiting for input.

    second

    scanf("%s",&artist);
    

    is not correct, use

    scanf("%s", artist);
    

    instead, no need to pass the address, it's already an array!

    printf("Welcome, hit ENTER to start looking.\n");
    printf("Please enter for the word you wish to search for in our database !\n");
    scanf("%s",artist);
    if (fileexists == 1 && outexists == 1) {
        char found = 0;
        while (fscanf(file, "%s", secret) != EOF) {
            if (!strcmp(secret, artist)) {
                found = 1;
                break;
            }
        }
        fclose(file);
        if(found) {
            printf("FOUND IT \n");
        } else {
            printf("COULDNT FIND IT\n");
        }
    }