搜索電子郵件,我有一個文本文件,例如「file_with_email.txt」,其中包含以下電子郵件地址:在一個文本文件
crp.edu
src.net
abc.edu
我需要尋找一個電子郵件在給定的文本文件。我的代碼的問題是,當我輸入帶有完整域名的電子郵件地址時,例如,如果我搜索例如「abc.edu」,則它將以「發現電子郵件」的形式返回消息,這是正確的。
但是,如果我輸入的電子郵件地址中包含不完整或部分域名,例如「abc.ed」,作爲給定文件中未包含的輸入,它會輸出與「發現的電子郵件「即使沒有這樣的電子郵件。
此外,在某些情況下,電子郵件會像用戶輸入的「abc.edu.net」一樣。在這種情況下,我的代碼將打印與「找到的電子郵件」相同的輸出,它不包含在給定的文本文件中。我希望有任何幫助解決這個問題。
下面是我到目前爲止已經試過在一個文本文件中搜索電子郵件功能:
int search_mail(char *email)
{
FILE *fp;
int line = 1;
int number_of_match = 0;
char temp[512];
char *fname = "/home/file_with_email.txt";
if((fp=fopen(fname, "r"))==NULL)
{
return(-1);
}
while(fgets(temp, 512,fp) !=NULL)
{
fprintf(stdout, "Just read: %s\n", temp);
if(strstr(temp, email) !=NULL)
{
printf("\n The match is found in the file\n ");
//printf("\n %s \n", temp);
number_of_match++;
}
//line++;
}
if(number_of_match == 0)
printf("\n No result found");
//close the file if it is open.
if(fp)
{
fclose(fp);
}
}
這將有助於縮進代碼正確 –
你不需要'如果(FP)',因爲其始終將是真正 –
'fprintf中(stdout' - 'printf'會做 –