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

时间:2013-11-09 20:06:12

标签: c strncmp

所以我试图创建一个程序,在文件中搜索用户输入的单词。如果这是一场比赛,那就说'#34;找到它",如果找不到任何东西,它会说" COULDNT发现任何事情" (显然: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;

}

2 个答案:

答案 0 :(得分:0)

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

fscanf(file, "%s", secret)

修改

也看到了,

fclose(file);

只有一个if + else后,您在while循环内的fscanfstrncmp中关闭文件。不要这样做,在wile循环终止后关闭文件。

另外,只需使用strcmp代替strncmp

编辑2

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

首先摆脱getchar();

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

我总是在打印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");
    }
}

答案 1 :(得分:0)

嘿,我遇到了类似的问题,我尝试读取一个字符串(例如名称),并将其与用户输入或预先存储的字符串进行比较。但是过了一会儿,我意识到您必须做一些事情才能使其工作....

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

   }

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