I have two files blacklist.txt and email.txt. The blacklist.txt contains some domain names. The email.txt also contains a few domain names. I have to compare both files and find the domain names of blacklist.txt into email.txt using the strstr()
function. Following is the code I have written. The problem with this code is it returns me the output NULL
instead of the matched text/domain name.
#include <stdio.h>
#include <string.h>
#define MAXCHAR 1000
int main() {
FILE *fp, *fp1;
char str[MAXCHAR];
char str2[MAXCHAR];
char *result;
fp = fopen("blacklist.txt", "r");
fp1 = fopen("email.txt", "r");
if (fp == NULL || fp1 == NULL) {
printf("\n Cannot open one of the files %s %s \n", fp, fp1);
//return = 0;
if (fp != NULL) fclose(fp);
if (fp1 != NULL) fclose(fp1);
}
while (fgets(str, MAXCHAR, fp) != NULL || fgets(str2, MAXCHAR, fp1) != NULL)
//printf("%s, %s", str,str2);
fclose(fp);
fclose(fp1);
result = strstr(str, str2);
printf("The substring starting from the given string: %s", result);
return 0;
}
Here are some remarks on your code:
FILE*
pointers instead of the file names.while
loop is missing.strstr()
means the line from the second file s a suffix of the line from the first file. It is a domain match if the return value is the start of the buffer and it is a match for a subdomain if the previous character is a .
.Here is a modified version:
#include <stdio.h>
#include <string.h>
#define MAXCHAR 1000
int main() {
FILE *fp, *fp2;
char str[MAXCHAR];
char str2[MAXCHAR];
// open and check blacklist.txt
fp = fopen("blacklist.txt", "r");
if (fp == NULL) {
printf("Cannot open %s\n", "blacklist.txt");
return 1;
}
// open and check email.txt
fp2 = fopen("email.txt", "r");
if (fp2 == NULL) {
printf("Cannot open %s\n", "email.txt");
fclose(fp);
return 1;
}
// for each line in blacklist.txt
while (fgets(str, MAXCHAR, fp) != NULL) {
// restart from the beginning of email.txt
rewind(fp2);
// for each line of email.txt
while (fgets(str2, MAXCHAR, fp2) != NULL) {
// check for a domain match
char *p = strstr(str, str2);
if (p != NULL && (p == str || p[-1] == '.')) {
// compute the length of the domains (excluding the newline)
int n = strcspn(str, "\n");
int n2 = strcspn(str2, "\n");
// output the message with the matching domains
printf("domain match on %.*s for %.*s\n", n2, str2, n, str);
}
}
}
fclose(fp);
fclose(fp2);
return 0;
}