gpt4 book ai didi

c - 在另一个字符串中查找字符串

转载 作者:行者123 更新时间:2023-11-30 20:25:31 24 4
gpt4 key购买 nike

所以我有一个具有以下格式的 .txt 文件:

abc@example.com:number
cde@example.com:number
efg@example.ru:number
And another file with:
abc@example.com
efg@example.ru

我想使用第二个文件中的电子邮件来查找 abc@example.com:number ,并在第三个文件中打印出电子邮件。唯一的问题是 strstr 对我不起作用,它打印出所有行,因为我猜@example.com 存在于所有电子邮件中。我将第二个文件中的所有电子邮件保存在数组中,然后从第一个文件中逐行读取行,并使用 strstr。

    #include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
using namespace std;

int main()
{
FILE *f,*g,*h;
char x[80],y[1000][60];
char *a,*b;
int i,found,n;
i=0;
found=0;
f=fopen("input.txt","r");
g=fopen("registered.txt","r");
h=fopen("output.txt","w");
if(f==NULL)
{
perror("No input!\n");
exit(1);
}
if(g==NULL)
{
perror("No registered!\n");
exit(1);
}
while(!feof(g))
{
fscanf(g,"%s",&y[i]);
i++;
}
n=i;
while(!feof(f))
{
fscanf(f,"%s",&x);
a=x;
for(i=0;i<=n;i++)
{
b=y[i];
if (strstr(a,b)!=NULL)
{
fprintf(h,"%s\n",x);
found++;
}
}
}
fclose(f);
fclose(g);
fclose(h);
printf("Found Email:%d\n",found);

}

输入.txt:

miani@uniud.it:150995
lucaburiani@libero.it:30000
danybai@hotmail.it:160988
freuzz@alice.it:469375
giozazzu@tiscali.it:30013568

注册.txt:

miani@uniud.it
lucaburiani@libero.it
danybai@hotmail.it

如果有必要,我会使用 CodeBlocks。

最佳答案

问题代码损坏

通过最少的修复(这样我就可以在我严格的编译器标志下将其编译为 C 代码),并添加诊断打印,您的代码的这个变体:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
FILE *f, *g, *h;
char x[80], y[1000][60];
char *a, *b;
int i, found, n;
i = 0;
found = 0;
f = fopen("input.txt", "r");
g = fopen("registered.txt", "r");
h = fopen("output.txt", "w");
if (f == NULL)
{
perror("No input!\n");
exit(1);
}
if (g == NULL)
{
perror("No registered!\n");
exit(1);
}
while (!feof(g))
{
fscanf(g, "%s", y[i]);
printf("Registered: [%s]\n", y[i]);
i++;
}
n = i;
while (!feof(f))
{
fscanf(f, "%s", x);
printf("Scanned: [%s]\n", x);
a = x;
for (i = 0; i <= n; i++)
{
b = y[i];
printf("Find: does [%s] contain [%s]?\n", a, b);
if (strstr(a, b) != NULL)
{
printf("Found: [%s] does contain [%s]!\n", a, b);
fprintf(h, "%s\n", x);
found++;
}
}
}
fclose(f);
fclose(g);
fclose(h);
printf("Found Email:%d\n", found);
}

产生此输出(给定您的数据):

Registered: [miani@uniud.it]
Registered: [lucaburiani@libero.it]
Registered: [danybai@hotmail.it]
Registered: []
Scanned: [miani@uniud.it:150995]
Find: does [miani@uniud.it:150995] contain [miani@uniud.it]?
Found: [miani@uniud.it:150995] does contain [miani@uniud.it]!
Find: does [miani@uniud.it:150995] contain [lucaburiani@libero.it]?
Find: does [miani@uniud.it:150995] contain [danybai@hotmail.it]?
Find: does [miani@uniud.it:150995] contain []?
Found: [miani@uniud.it:150995] does contain []!
Find: does [miani@uniud.it:150995] contain []?
Found: [miani@uniud.it:150995] does contain []!
Scanned: [lucaburiani@libero.it:30000]
Find: does [lucaburiani@libero.it:30000] contain [miani@uniud.it]?
Find: does [lucaburiani@libero.it:30000] contain [lucaburiani@libero.it]?
Found: [lucaburiani@libero.it:30000] does contain [lucaburiani@libero.it]!
Find: does [lucaburiani@libero.it:30000] contain [danybai@hotmail.it]?
Find: does [lucaburiani@libero.it:30000] contain []?
Found: [lucaburiani@libero.it:30000] does contain []!
Find: does [lucaburiani@libero.it:30000] contain []?
Found: [lucaburiani@libero.it:30000] does contain []!
Scanned: [danybai@hotmail.it:160988]
Find: does [danybai@hotmail.it:160988] contain [miani@uniud.it]?
Find: does [danybai@hotmail.it:160988] contain [lucaburiani@libero.it]?
Find: does [danybai@hotmail.it:160988] contain [danybai@hotmail.it]?
Found: [danybai@hotmail.it:160988] does contain [danybai@hotmail.it]!
Find: does [danybai@hotmail.it:160988] contain []?
Found: [danybai@hotmail.it:160988] does contain []!
Find: does [danybai@hotmail.it:160988] contain []?
Found: [danybai@hotmail.it:160988] does contain []!
Scanned: [freuzz@alice.it:469375]
Find: does [freuzz@alice.it:469375] contain [miani@uniud.it]?
Find: does [freuzz@alice.it:469375] contain [lucaburiani@libero.it]?
Find: does [freuzz@alice.it:469375] contain [danybai@hotmail.it]?
Find: does [freuzz@alice.it:469375] contain []?
Found: [freuzz@alice.it:469375] does contain []!
Find: does [freuzz@alice.it:469375] contain []?
Found: [freuzz@alice.it:469375] does contain []!
Scanned: [giozazzu@tiscali.it:30013568]
Find: does [giozazzu@tiscali.it:30013568] contain [miani@uniud.it]?
Find: does [giozazzu@tiscali.it:30013568] contain [lucaburiani@libero.it]?
Find: does [giozazzu@tiscali.it:30013568] contain [danybai@hotmail.it]?
Find: does [giozazzu@tiscali.it:30013568] contain []?
Found: [giozazzu@tiscali.it:30013568] does contain []!
Find: does [giozazzu@tiscali.it:30013568] contain []?
Found: [giozazzu@tiscali.it:30013568] does contain []!
Scanned: [giozazzu@tiscali.it:30013568]
Find: does [giozazzu@tiscali.it:30013568] contain [miani@uniud.it]?
Find: does [giozazzu@tiscali.it:30013568] contain [lucaburiani@libero.it]?
Find: does [giozazzu@tiscali.it:30013568] contain [danybai@hotmail.it]?
Find: does [giozazzu@tiscali.it:30013568] contain []?
Found: [giozazzu@tiscali.it:30013568] does contain []!
Find: does [giozazzu@tiscali.it:30013568] contain []?
Found: [giozazzu@tiscali.it:30013568] does contain []!
Found Email:15

请注意 while (!feof(file)) is always wrong 。您检查了输入文件是否已打开(良好);您没有检查输出文件是否已打开(错误)。

为了调试这个,首先要做的就是在读取数据时打印数据,以便您知道程序看到了什么。令人惊讶的是,计算机经常会看到与你想象的不同的东西。但这是最基本的调试技术之一。

因为您没有正确测试输入,并且因为您有 for(i=0;i<=n;i++) ,并且由于您的数组大部分已归零,因此您最终会尝试查看数据中是否找到空字符串,而且每次都是如此。如果您显示在 printf() 中比较的数据,您可以很容易地发现空字符串。陈述。字符串输出周围的方括号(或任何括号字符)可帮助您发现字符串中的意外字符,例如尾随空格或嵌入的回车符 ( '\r' ) 字符或换行符 ( '\n' )。

固定代码

远非完美,但明显更好:

#include<stdio.h>
#include<stdlib.h>
#include <string.h>

int main(void)
{
FILE *f, *g, *h;
char x[80], y[1000][60];
char *a, *b;
int i, found, n;
i = 0;
found = 0;
f = fopen("input.txt", "r");
g = fopen("registered.txt", "r");
h = fopen("output.txt", "w");
if (f == NULL)
{
perror("No input.txt!\n");
exit(1);
}
if (g == NULL)
{
perror("No registered.txt!\n");
exit(1);
}
if (h == NULL)
{
perror("No output.txt!\n");
exit(1);
}
while (fscanf(g, "%s", y[i]) == 1)
{
printf("Registered: [%s]\n", y[i]);
i++;
}
n = i;
while (fscanf(f, "%s", x) == 1)
{
printf("Scanned: [%s]\n", x);
a = x;
for (i = 0; i < n; i++)
{
b = y[i];
printf("Find: does [%s] contain [%s]\n", a, b);
if (strstr(a, b) != NULL)
{
printf("Match: %s\n", x);
fprintf(h, "%s\n", x);
found++;
break;
}
}
}
fclose(f);
fclose(g);
fclose(h);
printf("Found Email: %d\n", found);
}

示例输出:

Registered: [miani@uniud.it]
Registered: [lucaburiani@libero.it]
Registered: [danybai@hotmail.it]
Scanned: [miani@uniud.it:150995]
Find: does [miani@uniud.it:150995] contain [miani@uniud.it]
Match: miani@uniud.it:150995
Scanned: [lucaburiani@libero.it:30000]
Find: does [lucaburiani@libero.it:30000] contain [miani@uniud.it]
Find: does [lucaburiani@libero.it:30000] contain [lucaburiani@libero.it]
Match: lucaburiani@libero.it:30000
Scanned: [danybai@hotmail.it:160988]
Find: does [danybai@hotmail.it:160988] contain [miani@uniud.it]
Find: does [danybai@hotmail.it:160988] contain [lucaburiani@libero.it]
Find: does [danybai@hotmail.it:160988] contain [danybai@hotmail.it]
Match: danybai@hotmail.it:160988
Scanned: [freuzz@alice.it:469375]
Find: does [freuzz@alice.it:469375] contain [miani@uniud.it]
Find: does [freuzz@alice.it:469375] contain [lucaburiani@libero.it]
Find: does [freuzz@alice.it:469375] contain [danybai@hotmail.it]
Scanned: [giozazzu@tiscali.it:30013568]
Find: does [giozazzu@tiscali.it:30013568] contain [miani@uniud.it]
Find: does [giozazzu@tiscali.it:30013568] contain [lucaburiani@libero.it]
Find: does [giozazzu@tiscali.it:30013568] contain [danybai@hotmail.it]
Found Email: 3

关于c - 在另一个字符串中查找字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28516794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com