Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中比较用户输入字符串与文件中的字符串_C_Strncmp - Fatal编程技术网

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

如何在C中比较用户输入字符串与文件中的字符串,c,strncmp,C,Strncmp,因此,我试图制作一个程序,在文件中搜索用户输入的单词。如果有匹配项,它会说“找到了”,如果它什么都找不到,它会说“什么都找不到”(显然是p)。我不清楚如何在文件中扫描用户选择的单词(通过scanf写入) 这是我的(非功能性)代码。谢谢你的建议 #include "stdafx.h" #include <conio.h> #include <iostream> #include <string.h> #include <string> #define

因此,我试图制作一个程序,在文件中搜索用户输入的单词。如果有匹配项,它会说“找到了”,如果它什么都找不到,它会说“什么都找不到”(显然是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;
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#定义slovo 255
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
int i;
查秘密[斯洛伐克语];
int outexists,fileexists;
系统(“颜色02”);
setlocale(LC_ALL,“”);
FILE*out=fopen(“additions.txt”、“w+”);
如果(!out)
{
perror(“additions.txt”);
getchar();
返回1;
}
其他的
{
外部存在=1;
}
FILE*FILE=fopen(“db.txt”,“r”);
如果(!文件)
{
perror(“db.txt”);
getchar();
返回1;
}
其他的
{
fileexists=1;
}
木炭艺术家[斯洛文尼亚];
printf(“欢迎,点击回车开始查看。\n”);
getchar();
printf(“请输入您希望在我们的数据库中搜索的单词!
\n”);
scanf(“%s”和“艺术家”);
**/*此部分*/**如果(fileexists==1&&outexists==1)
{
while(fscanf(文件,“%c”,secret)!=EOF){
{如果(!strncmp(秘密,艺术家,sizeof(艺术家)))
{
printf(“找到它\n”);
fclose(文件);
}
其他的
{
printf(“找不到它\n”);
fclose(文件);
}
}
}
}
printf(“您希望添加哪个单词?\n”);
scanf(“%s”,艺术家);
fprintf(输出,“%s”,艺术家);
printf(“完成!\n”);
getchar();
fclose(文件);
fclose(out);
getchar();
返回0;

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

fscanf(file, "%s", secret)
编辑

我也看到了

fclose(file);
仅在一个
fscanf
+
strncmp
之后,即可关闭while循环中的
if
else
中的文件。否则,请在wile循环终止后关闭文件

也可以简单地使用strcmp而不是strncmp

编辑2

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

首先去掉
getchar()

我总是在
欢迎之后键入要搜索的键,点击ENTER开始查找。
会被打印出来,当点击ENTER时,不会读取任何内容,因为
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");
   }

   }

#包括
#包括
#包括
int main()
{
int i;
int j;
int-y;
字符名_read[100];
char-Testarr[100];
FILE*FILE=fopen(“C:/Documents/readfile”,“w+”;//打开文本文件进行写入
fprintf(文件“George |\n”);//注意我写的名字是“George”,并在末尾加了一个“|”
fclose(文件);
char name_stored[100]=“George”//存储我们要查找的名称
字符str[100]=“”;
FILE*ptr=fopen(“C:/Documents/readfile”,“r”);//打开文件进行读取
printf(“读取变量\n”);
//循环下面的内容,阅读多行内容
fgets(name_read,100,ptr);//从文件中读取名称

对于(j=0;jIt仍然说“找不到单词”。(谢谢你的回复tho。我试图解决这个问题-所以这行现在看起来是这样的:(!strcmp(secret,artist)),文件只是在循环之后才关闭。但是它仍然找不到单词:(好的,所以我按照你的建议做了,并且(很抱歉这么愚蠢)我仍然找不到一个词!我的db.txt在一列中有乐队/艺术家的名字。其中一个艺术家叫“约翰”,但当我搜索“约翰”时它说它什么也没找到。编辑:我找到了。现在它只查找文件中的第一行,这意味着它只检查文件中的名字,然后打印出结果。我假设我必须使用某种循环?@rar我想我已经提到了……你已经有一个while循环了,请检查我的编辑。所以明显的错误是st问题:
使用名称空间std;
在标记为
c
的程序中做了什么?对于任何不寻常的事情,我很抱歉,我是编程新手。根据我在互联网上的发现,这就是我所做的。我将编辑标记。老实说,标记不是问题;它是源代码。对于所有的意图都是C++程序,包括C++,但实际上没有使用C++标准库中的任何东西。如果允许使用C++标准库,这个任务变得容易得多。如果这是C程序,那么这些工件就不应该在源代码中。希望这是有意义的。
#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");
   }

   }