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

Find string in another string

所以我有一个具有以下格式的 .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 全部 emails.I 我将第二个文件中的所有电子邮件保存在一个数组中,然后逐行从第一个文件中读取,并使用 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);

}

input.txt:

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

registered.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() 语句中显示正在比较的数据,则可以很容易地发现空字符串。字符串输出周围的方括号(或任何括号字符)可帮助您发现意外字符,例如尾随空格或嵌入式回车 return ('\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