-2

So I have a .txt file with the following format:

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

And I want to using the email from second file to find the abc@example.com:number ,and print out the email in a third file .Only problem that strstr doesnt work for me ,it print out all the lines ,because I guess the @example.com is in all emails.I am saving all the emails from second file in a array ,then read from first file one by one the lines ,and use 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

I'm using CodeBlocks if it matter.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
GregMerk
  • 19
  • 3
  • 5
    As you use C++, you may use `std::string` – Jarod42 Feb 14 '15 at 14:54
  • 3
    ... an ``, and `` etc, and [fix this regardless: `while (!feof(g))`](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – WhozCraig Feb 14 '15 at 15:11
  • one error is in you condition, `i<=n` should be `i – sp2danny Feb 14 '15 at 15:14
  • 1
    `strstr` also want the string to search for last, so the call should be `strstr(b,a)` – sp2danny Feb 14 '15 at 15:22
  • You've tagged the question as C; you've coded the body of the question in C; but you included C++ headers at the top. Is it you or me who is confused by this? Whatever you're up to, please note that [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). You checked the input file were open (good); you didn't check that the output file was open (bad). – Jonathan Leffler Feb 14 '15 at 15:30
  • And for debugging this, the first thing to do is print the data as it is read, so that you know what the program is seeing. It is surprising how often the computer sees something other than what you thought it was going to see. But it is one of the most basic debugging techniques. – Jonathan Leffler Feb 14 '15 at 15:32
  • @JonathanLeffler I have the c++ header included as I tried to use some C++ functions and failed ,fixed the while problem .About the output file ,do I need to check if it's open ? why ? I only want to write in it ,if the file doesn't exist it will be created . – GregMerk Feb 14 '15 at 16:11
  • Because you don't test the inputs correctly, and because you have `for(i=0;i<=n;i++)`, and because your array is mostly zeroed, you end up trying to see if the empty string is found in your data, and it is, every time. You can spot the empty string easily enough if you show the data that is being compared in a `printf()` statement. I used: `printf("Find: does [%s] contain [%s]?\n", a, b);` immediately before the `strstr()` test, and `printf("Found: [%s] does contain [%s]!\n", a, b);` in the block afterwards. I also printed out each line of input. – Jonathan Leffler Feb 14 '15 at 16:11
  • You can just as easily fail to create a file for writing as fail to open one for reading. The file may already exist but be readonly; the name may already exist but it is actually a directory name; you may not have write permission in the directory; there might not be any inodes to spare so no new files can be created on the file system; the phase of the moon might be wrong while Mercury is in trine to Pluto. – Jonathan Leffler Feb 14 '15 at 16:14
  • @sp2danny fixed the condition, but if I put strstr(b,a) instead of strstr(a,b) ,there is nothing returned – GregMerk Feb 14 '15 at 16:16

1 Answers1

1

Broken code from question instrumented

With a bare minimum of fixes (so I can get it to compile as C code under my stringent compiler flags), and with diagnostic printing added, this variant of your code:

#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);
}

produces this output (given your data):

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

Please note that while (!feof(file)) is always wrong. You checked the input file were open (good); you didn't check that the output file was open (bad).

For debugging this, the first thing to do is print the data as it is read, so that you know what the program is seeing. It is surprising how often the computer sees something other than what you thought it was going to see. But it is one of the most basic debugging techniques.

Because you don't test the inputs correctly, and because you have for(i=0;i<=n;i++), and because your array is mostly zeroed, you end up trying to see if the empty string is found in your data, and it is, every time. You can spot the empty string easily enough if you show the data that is being compared in a printf() statement. The square brackets (or any bracketing characters) around the string outputs helps you spot unexpected characters, such as trailing spaces or embedded carriage return ('\r') characters or newline ('\n') in the strings.

Fixed code

Far from perfect, but demonstrably better:

#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);
}

Sample output:

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
Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278