简体   繁体   English

如何将用户输入字符串与C文件中的字符串进行比较

[英]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).如果有匹配项,它会说“找到它”,如果找不到任何东西,它会说“找不到任何东西”(显然是:p)。 Im not clear at how to scan the file for the word that the user chooses (writes through scanf).我不清楚如何扫描文件以查找用户选择的单词(通过 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;

} }

You probably need the conversion pattern %s instead of %c , the latter read only one char.您可能需要转换模式%s而不是%c ,后者只读取一个字符。 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 .在只有一个fscanf + strncmp之后,您在 while 循环内的ifelse关闭文件。 do not do that, close the file after the wile loop has terminated.不要这样做,在 wile 循环终止后关闭文件。

Also simply use strcmp instead of strncmp .也只需使用strcmp而不是strncmp

EDIT 2编辑 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();首先摆脱那个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.我总是在Welcome, hit ENTER to start looking.之后键入要搜索的键,然后按Welcome, hit ENTER to start looking. is printed and when hitting enter, nothing is read because getchar() was waiting for input.被打印出来,当按下回车键时,没有读取任何内容,因为getchar()正在等待输入。

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");
    }
}

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.但是请注意,如果您想读取多行名称,您可以实现您想要的任何循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM