繁体   English   中英

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

[英]How to compare user input string with string in a file in C

所以我试图制作一个程序,在文件中搜索用户输入的单词。 如果有匹配项,它会说“找到它”,如果找不到任何东西,它会说“找不到任何东西”(显然是:p)。 我不清楚如何扫描文件以查找用户选择的单词(通过 scanf 写入)。

这是我的(非功能性)代码。 感谢您的任何建议!

#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;

}

您可能需要转换模式%s而不是%c ,后者只读取一个字符。 前者读取一个字符串。

fscanf(file, "%s", secret)

编辑

也刚看到,

fclose(file);

在只有一个fscanf + strncmp之后,您在 while 循环内的ifelse关闭文件。 不要这样做,在 wile 循环终止后关闭文件。

也只需使用strcmp而不是strncmp

编辑 2

因此,在复制粘贴你的“棘手”代码后,顺便说一句,将我的控制台字体颜色变成绿色,我能够在文本文件中找到一个单词:

首先摆脱那个getchar();

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

我总是在Welcome, hit ENTER to start looking.之后键入要搜索的键,然后按Welcome, hit ENTER to start looking. 被打印出来,当按下回车键时,没有读取任何内容,因为getchar()正在等待输入。

第二

scanf("%s",&artist);

不正确,使用

scanf("%s", artist);

相反,不需要传递地址,它已经是一个数组!

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

嘿,我遇到了类似的问题,我试图读取一个字符串,例如一个名称,并将其与用户输入或预先存储的字符串进行比较。 然而过了一段时间我意识到你必须做一些事情才能让它工作......

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

   }

但是请注意,如果您想读取多行名称,您可以实现您想要的任何循环。

暂无
暂无

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

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