0

所以我试图制作一个程序,在文件中搜索用户输入的单词。如果匹配,它会说“找到它”,如果它找不到任何东西,它会说“无法找到任何东西”(显然: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;

}

4

2 回答 2

0

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

fscanf(file, "%s", secret)

编辑

刚刚也看到了

fclose(file);

您只在一个+之后关闭了while 循环中if和内部的文件。不要那样做,在 wile 循环终止后关闭文件。elsefscanfstrncmp

也只需使用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");
    }
}
于 2013-11-09T20:12:18.080 回答
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");
   }

   }

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

于 2020-05-12T16:26:17.093 回答