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