如何根据搜索条件在c语言的文本文件中搜索匹配的记录。这些记录用新行隔开
我想在文本文件中搜索匹配的记录,并只返回匹配的记录。文本文件如下所示如何根据搜索条件在c语言的文本文件中搜索匹配的记录。这些记录用新行隔开,c,C,我想在文本文件中搜索匹配的记录,并只返回匹配的记录。文本文件如下所示 2 James 2020/12/12 M ass 1 elijah 22/1/2021 M assymptomatic 1 elijah 22/1/2021 M assymptomatic 2 James 2020/12/12 M ass 2 James 2020/12/12 M ass $ 它以$结尾。 我已经写了一个c程序,它应该搜索匹配的记录并只返回匹配的记录 c程序在这里 #include <stdio.h&g
2 James 2020/12/12 M ass
1 elijah 22/1/2021 M assymptomatic
1 elijah 22/1/2021 M assymptomatic
2 James 2020/12/12 M ass
2 James 2020/12/12 M ass
$
它以$
结尾。
我已经写了一个c程序,它应该搜索匹配的记录并只返回匹配的记录
c程序在这里
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100000
char seacrh_result[MAX_LEN + 1] ="";
int check(char tko[], char criteria[])
{
char *ret;
ret = strstr(tko,criteria);
if(ret != NULL)
{
return 1;
}
else
{
printf("Nothing\n");
return 0;
}
}
const char* search(FILE *ptr,char search_criteria[])
{
//open the file containing records for reading
char string[MAX_LEN + 1];
// get all the file contents and store them in a string
fscanf(ptr,"%[^$]s",&string[0]);
printf("Text file contents:\n%s\n",string);
//start
char delimeter[] = "\n";
char *token = strtok(string,delimeter);
char tk[200];
while(token != NULL)
{
strcpy(tk,token);
if(check(tk,search_criteria))
{
strcat(seacrh_result,tk);
}
token = strtok(NULL,delimeter);
}
//end
return seacrh_result;
}
int main()
{
FILE *pointer;
pointer = fopen("/home/elijah/Desktop/plan.txt","r");
char criteria[100];
fgets(criteria,sizeof(criteria),stdin);
const char* result = search(pointer,criteria);
printf("Resut:\n%s\n",result);
return 0;
}
问题是什么???我不知道您是如何读取文件的,但您应该使用getline读取文件中的所有内容,并使用strccompare来查找macthes模式示例
fd = fopen(name, "r");
if (!fd)
{
dprintf(STDERR_FILENO, "Error: Can't open file %s\n", name);
exit(EXIT_FAILURE);
}
for (line_n = 1; getline(&lineprt, &n, fd) != EOF; line_n++)
{
// you should search here
}
#解决
我在某人的帮助下找到了问题的解决方案,fgets
函数在标准输入中输入的字符串后面追加一个\n
。因此,在比较过程中,strstr
函数将永远不会找到匹配项。因此,您可以使用get
或scanf
。它为我解决了这个问题
下面是使用get
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 100000
char seacrh_result[MAX_LEN + 1] ="";
int check(char tko[], char criteria[])
{
char *ret;
ret = strstr(tko,criteria);
if(ret != NULL)
{
return 1;
}
else
{
//printf("Nothing\n");
return 0;
}
}
const char* search(FILE *ptr,char search_criteria[])
{
//open the file containing records for reading
char string[MAX_LEN + 1];
// get all the file contents and store them in a string
char c;
while(1)
{
c = fgetc(ptr);
if(feof(ptr))
{
break;
}
strncat(string,&c,1);
}
printf("Text file contents:\n%s\n",string);
//start
char delimeter[] = "\n";
char *token = strtok(string,delimeter);
char tk[200];
while(token != NULL)
{
strcpy(tk,token);
if(check(tk,search_criteria))
{
strcat(seacrh_result,tk);
strcat(seacrh_result,"\n");
}
token = strtok(NULL,delimeter);
}
//end
return seacrh_result;
}
int main()
{
FILE *pointer;
pointer = fopen("/home/elijah/Desktop/plan.txt","r");
char criteria[100];
gets(criteria,sizeof(criteria),stdin);
printf("%s",criteria);
const char* result = search(pointer,criteria);
printf("Resut:\n%s\n",result);
return 0;
}
运行代码后的结果输出
elijah@elijah-HP-255-G6-Notebook-PC:~/Desktop$ ./search
Okello Ivan
Okello IvanText file contents:
5 Okello Ivan 2020/10/11 False Positive Assymptomatic M John
6 Leku Davis 2020/02/03 Positive Assymptomatic M Malik Berry
5 Okello Ivan 2020/10/11 False Positive Assymptomatic M John
6 Leku Davis 2020/02/03 Positive Assymptomatic M Malik Berry
Resut:
5 Okello Ivan 2020/10/11 False Positive Assymptomatic M John
5 Okello Ivan 2020/10/11 False Positive Assymptomatic M John
程序未按要求执行。所以,调试它。在调试器中运行程序,逐行检查它,在运行时检查变量值和控制流。注意:如果读取新行,它将存储在缓冲区中。仅供参考,
%[^$]s
除非您的目的是获取所有字符而不是$
,否则s
不应该在那里,然后期待s
并跳过它(考虑一下)。设置符号格式说明符已经期望字符串。请参见此处]()@ElijahOkello如果你已经调试过它,那么你应该知道哪里出了问题。分享你在调试过程中发现的信息会很好。打印出标准字符串。我想你会发现它包含一个尾随的“\n”
如fgets
手册所述。因此strstr
将永远找不到任何匹配项,因为tko
不包含换行符。在调用搜索之前,尝试从标准
字符串中删除换行符。它如何回答问题?我不知道您是如何读取文件的。为什么不?它在操作代码中显示得很清楚。。
5 Okello Ivan 2020/10/11 False Positive Assymptomatic M John
6 Leku Davis 2020/02/03 Positive Assymptomatic M Malik Berry
5 Okello Ivan 2020/10/11 False Positive Assymptomatic M John
6 Leku Davis 2020/02/03 Positive Assymptomatic M Malik Berry
elijah@elijah-HP-255-G6-Notebook-PC:~/Desktop$ ./search
Okello Ivan
Okello IvanText file contents:
5 Okello Ivan 2020/10/11 False Positive Assymptomatic M John
6 Leku Davis 2020/02/03 Positive Assymptomatic M Malik Berry
5 Okello Ivan 2020/10/11 False Positive Assymptomatic M John
6 Leku Davis 2020/02/03 Positive Assymptomatic M Malik Berry
Resut:
5 Okello Ivan 2020/10/11 False Positive Assymptomatic M John
5 Okello Ivan 2020/10/11 False Positive Assymptomatic M John