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

Hello again,
[size=3][/size]How can I search in a file containing several lines for words that are themselves contained in another file??
Then I should put the word(s) (found) and the coresponding line, one by one, in a third file!



[color=Blue]Thank you in advance!![/color]
«1

Comments

  • : Hello again,
    : [size=3][/size]How can I search in a file containing several lines
    : for words that are themselves contained in another file??
    : Then I should put the word(s) (found) and the coresponding line, one
    : by one, in a third file!
    :
    :
    :
    : [color=Blue]Thank you in
    : advance!![/color]
    :


    Don't understand the problem. Would you please give us an example: what is in File #1 and File #2?

    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
  • : : Hello again,
    : : [size=3][/size]How can I search in a file containing several lines
    : : for words that are themselves contained in another file??
    : : Then I should put the word(s) (found) and the coresponding line, one
    : : by one, in a third file!
    : :
    : :
    : :
    : : [color=Blue]Thank you in
    : : advance!![/color]
    : :

    :
    :
    : Don't understand the problem. Would you please give us an example:
    : what is in File #1 and File #2?
    :
    : =============================================
    : never lie -- the government doesn't like the competition. (Author
    : unknown)

    [size=3]Let's say that:

    FILE1:
    "I am programming using C language
    but I prefer Java
    maybe php is my favorite"

    FILE2:
    " Java
    using
    php"
    FILE 1 and 2 is given and I should produce:

    FILE 3:
    " Java = but I prefer Java
    using = I am programming using C language
    php = maybe php is my favorite " [/size]

    [size=4][color=Blue]Thank you again for your time, stober[/color][/size]
  • :
    : FILE1:
    : "I am programming using C language
    : but I prefer Java
    : maybe php is my favorite"
    :
    : FILE2:
    : " Java
    : using
    : php"
    : FILE 1 and 2 is given and I should produce:
    :
    : FILE 3:
    : " Java = but I prefer Java
    : using = I am programming using C language
    : php = maybe php is my favorite " [/size]
    :
    :


    Oh! That makes sense now. Start out by reading File 1 into an array of strings to make it easier and faster to search.

    Next, read each line of File 2. When read one line in File 2 search the array of strings read from File 1 for that text from File 2. The strstr() function will be helpful for that. When found save the info to File 3. I could give you the code, but I won't.
    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
  • : :
    : : FILE1:
    : : "I am programming using C language
    : : but I prefer Java
    : : maybe php is my favorite"
    : :
    : : FILE2:
    : : " Java
    : : using
    : : php"
    : : FILE 1 and 2 is given and I should produce:
    : :
    : : FILE 3:
    : : " Java = but I prefer Java
    : : using = I am programming using C language
    : : php = maybe php is my favorite " [/size]
    : :
    : :
    :
    :
    : Oh! That makes sense now. Start out by reading File 1 into an
    : array of strings to make it easier and faster to search.
    :
    : Next, read each line of File 2. When read one line in File 2 search
    : the array of strings read from File 1 for that text from File 2.
    : The strstr() function will be helpful for that. When found save the
    : info to File 3. I could give you the code, but I won't.
    : =============================================
    : never lie -- the government doesn't like the competition. (Author
    : unknown)

    [size=3]Thank you,thank you,thank. I'll try to sort things out and I will post again if I have any problem![/size]
  • : : :
    : : : FILE1:
    : : : "I am programming using C language
    : : : but I prefer Java
    : : : maybe php is my favorite"
    : : :
    : : : FILE2:
    : : : " Java
    : : : using
    : : : php"
    : : : FILE 1 and 2 is given and I should produce:
    : : :
    : : : FILE 3:
    : : : " Java = but I prefer Java
    : : : using = I am programming using C language
    : : : php = maybe php is my favorite " [/size]
    : : :
    : : :
    : :
    : :
    : : Oh! That makes sense now. Start out by reading File 1 into an
    : : array of strings to make it easier and faster to search.
    : :
    : : Next, read each line of File 2. When read one line in File 2 search
    : : the array of strings read from File 1 for that text from File 2.
    : : The strstr() function will be helpful for that. When found save the
    : : info to File 3. I could give you the code, but I won't.
    : : =============================================
    : : never lie -- the government doesn't like the competition. (Author
    : : unknown)
    :
    : [size=3]Thank you,thank you,thank. I'll try to sort things out and I
    : will post again if I have any problem![/size]
    :

    Can you please tell me how to read the files to a string? I can't seem to manage it!
  • :
    : Can you please tell me how to read the files to a string? I can't
    : seem to manage it!
    :


    post your code. It should go something like this
    [code]
    char iobuf[255];
    FILE* fp = fopen("filename.txt","r");
    if( fp != NULL)
    {
    while( fgets(iobuf,sizeof(iobuf),fp) )
    {
    // do something here
    }
    fclose(fp);
    }
    [/code]

    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
  • : :
    : : Can you please tell me how to read the files to a string? I can't
    : : seem to manage it!
    : :
    :
    :
    : post your code. It should go something like this
    : [code]:
    : char iobuf[255];
    : FILE* fp = fopen("filename.txt","r");
    : if( fp != NULL)
    : {
    : while( fgets(iobuf,sizeof(iobuf),fp) )
    : {
    : // do something here
    : }
    : fclose(fp);
    : }
    : [/code]:
    :
    : =============================================
    : never lie -- the government doesn't like the competition. (Author
    : unknown)



    [size=2] #include

    FILE* fin1;
    FILE* fin2;
    char buf1[500], buf2[500];
    char arr1[500];
    int i;

    main()
    {

    fin1= fopen("c:\b.txt","r");
    fin2=fopen("c:\c.txt","r");
    if (fin1!=NULL)
    {
    while (fgets(buf1,sizeof(buf1),fin1) )

    { arr1[i]=buf1[i];

    i++;}
    fclose(fin1);
    }

    }
    [/size]
    [size=3][color=Blue]But it doesn't work this way?Pleazzz!What am id doing wrong?[/color][/size]
  • : : :
    : : : Can you please tell me how to read the files to a string? I can't
    : : : seem to manage it!
    : : :
    : :
    : :
    : : post your code. It should go something like this
    : : [code]: :
    : : char iobuf[255];
    : : FILE* fp = fopen("filename.txt","r");
    : : if( fp != NULL)
    : : {
    : : while( fgets(iobuf,sizeof(iobuf),fp) )
    : : {
    : : // do something here
    : : }
    : : fclose(fp);
    : : }
    : : [/code]: :
    : :
    : : =============================================
    : : never lie -- the government doesn't like the competition. (Author
    : : unknown)
    :
    :
    :
    : [size=2] #include
    :
    : FILE* fin1;
    : FILE* fin2;
    : char buf1[500], buf2[500];
    : char arr1[500];
    : int i;
    :
    : main()
    : {
    :
    : fin1= fopen("c:\b.txt","r");
    : fin2=fopen("c:\c.txt","r");
    : if (fin1!=NULL)
    : {
    : while (fgets(buf1,sizeof(buf1),fin1) )
    :
    : { arr1[i]=buf1[i];
    :
    : i++;}
    : fclose(fin1);
    : }
    :
    : }
    : [/size]
    : [size=3][color=Blue]But it doesn't work this way?Pleazzz!What am id
    : doing wrong?[/color][/size]
    :
    You're not searching through the text file or saving the resulting array.
  • : : : :
    : : : : Can you please tell me how to read the files to a string? I can't
    : : : : seem to manage it!
    : : : :
    : : :
    : : :
    : : : post your code. It should go something like this
    : : : [code]: : :
    : : : char iobuf[255];
    : : : FILE* fp = fopen("filename.txt","r");
    : : : if( fp != NULL)
    : : : {
    : : : while( fgets(iobuf,sizeof(iobuf),fp) )
    : : : {
    : : : // do something here
    : : : }
    : : : fclose(fp);
    : : : }
    : : : [/code]: : :
    : : :
    : : : =============================================
    : : : never lie -- the government doesn't like the competition. (Author
    : : : unknown)
    : :
    : :
    : :
    : : [size=2] #include
    : :
    : : FILE* fin1;
    : : FILE* fin2;
    : : char buf1[500], buf2[500];
    : : char arr1[500];
    : : int i;
    : :
    : : main()
    : : {
    : :
    : : fin1= fopen("c:\b.txt","r");
    : : fin2=fopen("c:\c.txt","r");
    : : if (fin1!=NULL)
    : : {
    : : while (fgets(buf1,sizeof(buf1),fin1) )
    : :
    : : { arr1[i]=buf1[i];
    : :
    : : i++;}
    : : fclose(fin1);
    : : }
    : :
    : : }
    : : [/size]
    : : [size=3][color=Blue]But it doesn't work this way?Pleazzz!What am id
    : : doing wrong?[/color][/size]
    : :
    : You're not searching through the text file or saving the resulting
    : array.
    :
    How do I do that Please?
  • : : : : :
    : : : : : Can you please tell me how to read the files to a string? I can't
    : : : : : seem to manage it!
    : : : : :
    : : : :
    : : : :
    : : : : post your code. It should go something like this
    : : : : [code]: : : :
    : : : : char iobuf[255];
    : : : : FILE* fp = fopen("filename.txt","r");
    : : : : if( fp != NULL)
    : : : : {
    : : : : while( fgets(iobuf,sizeof(iobuf),fp) )
    : : : : {
    : : : : // do something here
    : : : : }
    : : : : fclose(fp);
    : : : : }
    : : : : [/code]: : : :
    : : : :
    : : : : =============================================
    : : : : never lie -- the government doesn't like the competition. (Author
    : : : : unknown)
    : : :
    : : :
    : : :
    : : : [size=2] #include
    : : :
    : : : FILE* fin1;
    : : : FILE* fin2;
    : : : char buf1[500], buf2[500];
    : : : char arr1[500];
    : : : int i;
    : : :
    : : : main()
    : : : {
    : : :
    : : : fin1= fopen("c:\b.txt","r");
    : : : fin2=fopen("c:\c.txt","r");
    : : : if (fin1!=NULL)
    : : : {
    : : : while (fgets(buf1,sizeof(buf1),fin1) )
    : : :
    : : : { arr1[i]=buf1[i];
    : : :
    : : : i++;}
    : : : fclose(fin1);
    : : : }
    : : :
    : : : }
    : : : [/size]
    : : : [size=3][color=Blue]But it doesn't work this way?Pleazzz!What am id
    : : : doing wrong?[/color][/size]
    : : :
    : : You're not searching through the text file or saving the resulting
    : : array.
    : :
    : How do I do that Please?
    :
    You can put stuff onto a file using [link=http://www.cplusplus.com/reference/clibrary/cstdio/fputs.html]fputs.[/link]
    As for searching it, the first step is to find all the words that are being searched for, and store them in an array of strings. Then, somehow actually look through the buffer for the words, and add the findings to file C.
  • : : : : : :
    : : : : : : Can you please tell me how to read the files to a string? I can't
    : : : : : : seem to manage it!
    : : : : : :
    : : : : :
    : : : : :
    : : : : : post your code. It should go something like this
    : : : : : [code]: : : : :
    : : : : : char iobuf[255];
    : : : : : FILE* fp = fopen("filename.txt","r");
    : : : : : if( fp != NULL)
    : : : : : {
    : : : : : while( fgets(iobuf,sizeof(iobuf),fp) )
    : : : : : {
    : : : : : // do something here
    : : : : : }
    : : : : : fclose(fp);
    : : : : : }
    : : : : : [/code]: : : : :
    : : : : :
    : : : : : =============================================
    : : : : : never lie -- the government doesn't like the competition. (Author
    : : : : : unknown)
    : : : :
    : : : :
    : : : :
    : : : : [size=2] #include
    : : : :
    : : : : FILE* fin1;
    : : : : FILE* fin2;
    : : : : char buf1[500], buf2[500];
    : : : : char arr1[500];
    : : : : int i;
    : : : :
    : : : : main()
    : : : : {
    : : : :
    : : : : fin1= fopen("c:\b.txt","r");
    : : : : fin2=fopen("c:\c.txt","r");
    : : : : if (fin1!=NULL)
    : : : : {
    : : : : while (fgets(buf1,sizeof(buf1),fin1) )
    : : : :
    : : : : { arr1[i]=buf1[i];
    : : : :
    : : : : i++;}
    : : : : fclose(fin1);
    : : : : }
    : : : :
    : : : : }
    : : : : [/size]
    : : : : [size=3][color=Blue]But it doesn't work this way?Pleazzz!What am id
    : : : : doing wrong?[/color][/size]
    : : : :
    : : : You're not searching through the text file or saving the resulting
    : : : array.
    : : :
    : : How do I do that Please?
    : :
    : You can put stuff onto a file using
    : [link=http://www.cplusplus.com/reference/clibrary/cstdio/fputs.html]f
    : puts.[/link]
    : As for searching it, the first step is to find all the words that
    : are being searched for, and store them in an array of strings. Then,
    : somehow actually look through the buffer for the words, and add the
    : findings to file C.
    :

    As for searching it, the first step is to find all the words that
    : are being searched for, [color=Red][size=3]and store them in an array of strings[/size][/color]. Then,
    : somehow actually look through the buffer for the words, and add the
    : findings to file C.

    I have understood that!!!I don't know how to store them in an array.That is the problem!! Thank you in advance once again!
  • :
    : I have understood that!!!I don't know how to store them in an
    : array.That is the problem!! Thank you in advance once again!
    :


    create an array of pointers and then fill that array with the words you want.
    [code]
    // create an array that will hold up to 255 words
    // you can change 255 to whatever you want as long
    // as its big enough to hold all the words. There are
    // other ways to do this but this is the simplest
    char *array[255] = {0};
    // this is a counter that indicates the number of words in the above array.
    int NumWords = 0;

    // Add a word to the array like this: Assume variable buf
    // is a character buffer that was read from a file

    // allocate memory for the word
    array[NumWords] = malloc( strlen(buf) + 1);
    // copy word into the array
    strcpy( array[NumWords], buf);
    // bump counter
    ++NumWords;

    [/code]
    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
  • : create an array of pointers and then fill that array with the words
    : you want.
    : [code]:
    : // create an array that will hold up to 255 words
    : // you can change 255 to whatever you want as long
    : // as its big enough to hold all the words. There are
    : // other ways to do this but this is the simplest
    : char *array[255] = {0};
    : // this is a counter that indicates the number of words in the above array.
    : int NumWords = 0;
    :
    : // Add a word to the array like this: Assume variable buf
    : // is a character buffer that was read from a file
    :
    : // allocate memory for the word
    : array[NumWords] = malloc( strlen(buf) + 1);
    : // copy word into the array
    : strcpy( array[NumWords], buf);
    : // bump counter
    : ++NumWords;
    :
    : [/code]:
    : =============================================
    : never lie -- the government doesn't like the competition. (Author
    : unknown)


    [size=3]This is my code so far. I then try to use the strstr function to search -even one- line but it doesn't work. Can you help me find the error??[/size]

    [size=2]#include
    #include
    #include

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

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

    for(i=0;i<countwords;i++) //show the array of file1
    printf("%s",arr1[i]);
    printf("
    ");
    if (fin2!=NULL)
    {
    while (fgets(buf2,sizeof(buf2),fin2) )

    ++LineCounter;
    if (LineCounter!=4){
    {
    for (i=0;i<4;i++){
    sol=strstr(buf2,arr1[i]);
    printf("%s",sol); } }

    if( sol == NULL ) printf( "Could not find %s in '%s' ",arr1[0], arr2[0] );
    printf( "Found a substring: '%s'
    ", sol );[/size]

    [color=Blue][size=3]Is't strstr supposed to stop in ''. How I will search the whole phrase??[/size][/color]

    Thanks A MILLION!!!
  • : [code]
    : while (fgets(buf2,sizeof(buf2),fin2) )
    :
    : ++LineCounter;
    : if (LineCounter!=4){
    : {
    : for (i=0;i<4;i++){
    : sol=strstr(buf2,arr1[i]);
    : printf("%s",sol); }
    : }
    :

    The problem is the above code is wrong. You don't need LineCounter
    variable because you already have counter variable when the first file
    was read. After reading a line from the second file you have to loop
    through all the lines that were read from the first file, your code is
    only looking at the first 4 lines and hopefully there are at least 4 lines.

    [code]
    while (fgets(buf2,sizeof(buf2),fin2) )
    {
    for(i = 0; i < counter; i++)
    {
    if( (sol=strstr(buf2,arr1[i])) != NULL)
    {
    printf("Found it!
    ");
    }
    }
    }
    [/code]


    I hope you realize that using the variable [b]i[/b] above will destroy the
    value of the loop counter in the previous loop you coded. I would suggest
    you just delete the previous loop using i counter because it serves no
    purpose to solving the solution to your problem.


    =============================================
    never lie -- the government doesn't like the competition. (Author unknown)
  • : : [code]:
    : : while (fgets(buf2,sizeof(buf2),fin2) )
    : :
    : : ++LineCounter;
    : : if (LineCounter!=4){
    : : {
    : : for (i=0;i<4;i++){
    : : sol=strstr(buf2,arr1[i]);
    : : printf("%s",sol); }
    : : }
    : :
    :
    : The problem is the above code is wrong. You don't need LineCounter
    : variable because you already have counter variable when the first file
    : was read. After reading a line from the second file you have to loop
    : through all the lines that were read from the first file, your code is
    : only looking at the first 4 lines and hopefully there are at least 4 lines.
    :
    : [code]:
    : while (fgets(buf2,sizeof(buf2),fin2) )
    : {
    : for(i = 0; i < counter; i++)
    : {
    : if( (sol=strstr(buf2,arr1[i])) != NULL)
    : {
    : printf("Found it!
    ");
    : }
    : }
    : }
    : [/code]:
    :
    :
    : I hope you realize that using the variable [b]i[/b] above will
    : destroy the
    : value of the loop counter in the previous loop you coded. I would
    : suggest
    : you just delete the previous loop using i counter because it serves
    : no
    : purpose to solving the solution to your problem.
    :
    :
    : =============================================
    : never lie -- the government doesn't like the competition. (Author
    : unknown)


    Thank you for that but even that way, the strstr doesn't work!!
    It doesn't return anything ("Found it") even though the two files have common words!
    [size=2]while (fgets(buf2,sizeof(buf2),fin2) )
    {
    for(i = 0; i < counter; i++)
    {
    if( (sol=strstr(buf2,arr1[i])) != NULL)
    {
    printf("Found it!
    ");
    }
    }[/size]

    Why is that???T-h-a-n-k y-o-u!
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