How can i search a file using another file?PLEASE!

2»

Comments

  • post a few lines from each file -- maybe strstr() is not what you need afterall, but impossible to say without seeing a little bit of each file.
    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
  • : post a few lines from each file -- maybe strstr() is not what you
    : need afterall, but impossible to say without seeing a little bit of
    : each file.
    : =============================================
    : never lie -- the government doesn't like the competition. (Author
    : unknown)


    This is a sample of 2 files I should produce the third.

    FILE1:
    : : "I am programming using C-language
    : : but I prefer Java
    : : maybe php is my favorite"
    : :
    : : FILE2:
    : : "Java
    : : using
    : : php"
    : :
    : : FILE 3 (created by the program):
    : : " Java = but I prefer Java
    : : using = I am programming using C language


    Please, any suggestions!! I got stuck here! Thank you!
  • you have a couple problems. First the contents of the files should not contain the quotes because the text [b]"Java[/b] does not exist in the first file due to the leading quote.

    Second, the function fgets() usually appends the CR/LF text record separator to the end of the string that is read in. So that needs to be stripped off before adding to the array and before using strstr(). After correcting the above problems and correcting program formatting style it works ok.

    I also removed all global variables and made them local to main(). If you are using an ancient 16-bit compiler such as Turbo C you may not be able to do that due to limited stack size.

    Variable buf2 was deleted because you can re-use buf1 and reduce memory requirements. You could have also used just one FILE pointer, but I didn't bother to change that.

    [code]
    #include
    #include
    #include

    char* readline(FILE* fp, char* inbuf, int bufsize)
    {
    // reads one line from a text file and strips off the
    // trailing '
    ' if it exists.

    int length;
    if( fgets(inbuf,bufsize,fp) == NULL)
    return NULL;
    length = strlen(inbuf) - 1;
    if( inbuf[length] == '
    ')
    inbuf[length] = 0;
    return inbuf;
    }

    int main()
    {
    FILE* fin1;
    FILE* fin2;
    char buf1[500];
    char *arr1[500]={0};
    char *sol;
    int i,counter;

    counter =0;
    fin1= fopen("d.txt","r");
    fin2=fopen("e.txt","r");
    if (fin1!=NULL)
    {
    while ( readline(fin1,buf1,sizeof(buf1)) )
    {
    arr1[counter] = (char*)malloc( strlen(buf1) + 1);
    strcpy( arr1[counter], buf1);
    ++counter;
    } //assigned the first file to an array
    fclose(fin1);
    }

    if (fin2!=NULL)
    {
    while (readline(fin2,buf1,sizeof(buf1)) )
    {
    for(i = 0; i < counter; i++)
    {
    if( (sol=strstr(arr1[i],buf1)) != NULL)
    {
    printf("%s
    ",arr1[i]);
    }
    }
    }
    fclose(fin2);
    }
    }

    [/code]

    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories