How can i search a file using another file?PLEASE! - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

noobgrnlnoobgrnl Posts: 19Member
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

  • stoberstober Posts: 9,765Member ✭✭✭
    : 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)
  • noobgrnlnoobgrnl Posts: 19Member
    : : 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]
  • stoberstober Posts: 9,765Member ✭✭✭
    :
    : 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)
  • noobgrnlnoobgrnl Posts: 19Member
    : :
    : : 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]
  • noobgrnlnoobgrnl Posts: 19Member
    : : :
    : : : 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!
  • stoberstober Posts: 9,765Member ✭✭✭
    :
    : 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)
  • noobgrnlnoobgrnl Posts: 19Member
    : :
    : : 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]
  • cactus1cactus1 Posts: 41Member
    : : :
    : : : 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.
  • noobgrnlnoobgrnl Posts: 19Member
    : : : :
    : : : : 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?
  • cactus1cactus1 Posts: 41Member
    : : : : :
    : : : : : 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.
«1
Sign In or Register to comment.