所以我有一个.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:号码,并在第三个文件中打印出电子邮件。只有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);
}
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
如果重要的话我会使用CodeBlock。
答案 0 :(得分:1)
只需要最少的修复(因此我可以在严格的编译器标志下将其编译为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