0

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;

}

Deanie
  • 2,316
  • 2
  • 19
  • 35
raz
  • 75
  • 2
  • 12
  • So the obvious first question: What is `` , ``, and `using namespace std;` doing in a program tagged **`c`**? – WhozCraig Nov 09 '13 at 20:42
  • I am sorry for any unusual things, I am a beginner in programming. And from what I could find on the internet this is what I've scrambled up. Will edit the tags. – raz Nov 09 '13 at 20:47
  • Honestly the tags aren't the problem; its the source. For all intents this is a C program, as your including, but not actually using, anything from the C++ standard library. If you're allowed to use the C++ standard library, this task becomes markedly easier. If this is supposed to be a C program, then those artifacts shouldn't be in the source. Hope that made sense. – WhozCraig Nov 09 '13 at 21:00

2 Answers2

0

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");
    }
}
A4L
  • 17,353
  • 6
  • 49
  • 70
  • It still says "Couldnt find the word". :( Thanks for the reply tho. – raz Nov 09 '13 at 20:15
  • I tried to fix that - so the line now looks like this : (!strcmp(secret, artist)) and the file is only closed after the loop. But it still doesnt find the word :( – raz Nov 09 '13 at 20:27
  • Okay, so I did what you suggested and (im sorry for being so annoyingly dumb) I still wasnt able to find a word ! my db.txt has names of bands/artist under each other in one column. One of the artist is called "John" but when i search for "John" it says it didnt find anything. – raz Nov 09 '13 at 21:07
  • EDIT: I figured it out. Now it only looks for the first line in the file which means it only checks the first name in the file and then prints out the result. Im assuming im gonna have to use some kind of a loop ? – raz Nov 09 '13 at 21:08
  • @rar I think I already mentioned that... you already have a while loop, check my edit. – A4L Nov 09 '13 at 21:12
  • Yeah I know, the file closing thing. But now, lets say i have 30 names in the .txt, it prints out 29 times "couldnt find it" and 1 time it prints "found it" (if i inject the right word ofc). – raz Nov 09 '13 at 21:17
  • @raz create a flag which you set to `1` if you find the key and move the printing outside the loop. Check my edit again. – A4L Nov 09 '13 at 21:26
  • Thanks alot mate. You were really helpful. You deserve a beer good sir ! – raz Nov 09 '13 at 21:34
0

hey i encountered a similar problem where i tried to read a string, for example a name, and compare it with the user input or a pre-stored string. However after a while i realised that you have to do a few things to make it work....

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

int main()
{
    int i;
    int j;
    int y;
    char name_read[100];
    char Testarr[100];
    FILE *file = fopen("C:/Documents/readfile", "w+");//opened text file for writing



    fprintf(file,"George|\n");// note i wrote the name "George" and put a '|' at the end


    fclose(file);

    char name_stored[100] = "George";//store the name we want to find
    char str[100] = "";

    FILE *ptr = fopen("C:/Documents/readfile","r");//open file for reading
 printf("reading variables\n");

//loop everything below for reading multiple lines
   fgets(name_read, 100, ptr);//read the name from the file



  for(j = 0; j<= 100; j++){
     if(name_read[j] == '|'){//check to see if we reached the marker '|'

        for(i = 0; i<j; i++){
            str[i] = name_read[i];//copy everything before the marker into str
           }

       }
  }

 printf("\n%s\n", str);

   if(!strcmp(str, name_stored)){//compare the name we read from the file with the name stored
    printf("match successful");
   }

   }

Note however that if you want to read multiple lines of names you can implement any loop you want.