• 0
Sign in to follow this  
tahinf

comparar dois arquivos

Question

olá!

estou tentando fazer um programa que lê um arquivo e procura nele certas palavras q está em outro arquivo. Está dando +- certo, só que a primeira palavra que está no arquivo chaves ele não encontra(mas está no outro arquivo)

#include <stdio.h>
#include <conio.h>

#include <stdlib.h>
#define max 100


int main(int argc, char *argv[])

{

    FILE * fp, *fp2;



    char ca[100], carc[100];


    if((fp=fopen("hello.txt","r"))==NULL)

    {

                                   printf("Erro");

                                   exit(1);

                                   }
    if((fp2=fopen("chaves.txt","r"))==NULL)

    {

                                   printf("Erro");

                                   exit(1);

                                   }



    fgets(ca,max,fp);
    fgets(carc,max,fp2);




    while((!feof(fp)))



    {              if(strstr(ca, carc)==NULL)
                    {  printf("\nnão encontrou\n");
                                          }
                    else
                    {
                        printf("%s",carc);

                     }




                   fgets(ca,max,fp);
                    fgets(carc,max,fp2);

}





  fclose(fp);
    fclose(fp2);
  system("PAUSE");

  return 0;

}

arquivo hello:

#include <stdio.h>

void

main()

{

printf ("Hello World!\n");

int i;

}

arquivo chaves:

void

int

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

agora ele lê o void, mas mesmo sendo iguais tá falando que não são e não tá indo pra próxima palavra que seria int e comparando no arquivo inteiro novamente. alguém pode ajudar?

#include <stdio.h>
#include <conio.h>

#include <stdlib.h>
#define max 100

FILE * fp, *fp2;

void open()
 {

    if((fp=fopen("hello.txt","r"))==NULL)

    {
        printf("Erro");
        exit(1);
    }
 }

 void open2(){
    if((fp2=fopen("chaves.txt","r"))==NULL)
    {
        printf("Erro");
        exit(1);
    }
 }

int main(int argc, char *argv[])

{


 open();
 open2();


    char ca[100], carc[100];


    fgets(ca,max,fp);
    fgets(carc,max,fp2);




    while((!feof(fp2)))
    {


        while((!feof(fp)))
        {
            printf("\n carc: %s",carc);
            printf("\n ca: %s",ca);

          if(strcmp(ca, carc)!=0)
                    {  printf("\n");
                                          }
                    else
                    {
                        printf("    ENCONTROU  %s",carc);

                     }


        fgets(ca,max,fp);
        }
              fclose(fp);
              open();
            fgets(ca,max,fp);
            fgets(carc,max,fp2);

}





  fclose(fp);
    fclose(fp2);
  system("PAUSE");

  return 0;

}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this