question

Programacao C comparar dois arquivos?

Olá. Por favor, alguém pode escrever um programa em C para comparar dois arquivos, byte por byte.
Por favor, use o Open () em vez de fopen (), Read () em vez de fread(). O programa nos diz que se o arquivos são idênticas ou diferentes.
resposta Resposta
Eu acho que eu mencionei antes, Open (), Read () e Close () são original K & R mas não ISO padrão C. Eles existem em POSIX, embora. De qualquer forma, eu suponho que você pode executar isso em uma linha de comando e usar dois parâmetros na linha de comando para especificar os nomes de arquivos. Não queria incomodar escrever instruções de impressão desejavam para solicitar os nomes dos arquivos. Então se você quer isso, você precisará adicioná-lo. Principalmente, este recebe o impulso de uma comparação, embora.

# incluem <stdio.h># incluem <io.h># incluem <fcntl.h>int main (int argc, char argv []) {int /mnt/lun1, lun2, c1, c2;      Char buf1, buf2;         if ( argc < 3 ) {             printf( "Requires two arguments for the two filenames.\n" );             return 0;         }         if ( (lun1= open( argv[1], O_RDONLY | O_BINARY )) == -1 ) {             printf( "Can't find %s to open.\n", argv[1] );             return 0;         }         if ( (lun2= open( argv[2], O_RDONLY | O_BINARY )) == -1 ) {             printf( "Can't find %s to open.\n", argv[2] );             close( lun1 );             return 0;         }         c1= read( lun1, &buf1, 1 );         c2= read( lun2, &buf2, 1 );         while ( c1 != 0 && c2 != 0 ) {             if ( buf1 != buf2 ) break;             c1= read( lun1, &buf1, 1 );             c2= read( lun2, &buf2, 1 );         }         close( lun1 );         close( lun2 );         if ( c1 == 0 && c2 == 0 )             printf( "Files match.\n" );         else             printf( "Files don't match %d %d.\n", c1, c2 );     return 0; }

I avoided using _read() and _close and _open, using the POSIX calls instead. Since there is no standard, you choose your own poison, I guess. Binary mode is used here to avoid "cooked mode" processing, which would not permit a genuine comparison of two files as identical.

EDIT: Take note that Ari's approach won't work for several reasons, even assuming that fopen() was okay with you. For one, the files aren't opened in binary mode in his example (at the time I'm reading it now.) For another, he gets characters in the loop control and uses && there. If the first file, let's say, is shorter than the second file his loop will exit prematurely and say that the files are the same, when they are not necessarily the same because the second file still may have more data in it. So, basically, it doesn't work as well as not meeting your request. He also doesn't compare against EOF. And he doesn't properly declare the return type of fgetc(), instead putting the values into 'char' variables, when he should be using int. It's good for learning about commonly made mistakes, though. It's densely packed with them. 3="" )="" {=""  =""  =""  =""  =""  =""  ="" printf(="" "requires="" two="" arguments="" for="" the="" two="" filenames.\n"="" );=""  =""  =""  =""  =""  =""  ="" return="" 0;=""  =""  =""  =""  ="" }=""  =""  =""  =""  ="" if="" (="" (lun1="open(" argv[1],="" o_rdonly="" |="" o_binary="" ))="=" -1="" )="" {=""  =""  =""  =""  =""  =""  ="" printf(="" "can't="" find="" %s="" to="" open.\n",="" argv[1]="" );=""  =""  =""  =""  =""  =""  ="" return="" 0;=""  =""  =""  =""  ="" }=""  =""  =""  =""  ="" if="" (="" (lun2="open(" argv[2],="" o_rdonly="" |="" o_binary="" ))="=" -1="" )="" {=""  =""  =""  =""  =""  =""  ="" printf(="" "can't="" find="" %s="" to="" open.\n",="" argv[2]="" );=""  =""  =""  =""  =""  =""  ="" close(="" lun1="" );=""  =""  =""  =""  =""  =""  ="" return="" 0;=""  =""  =""  =""  ="" }=""  =""  =""  =""  ="" c1="read(" lun1,="" &buf1,="" 1="" );=""  =""  =""  =""  ="" c2="read(" lun2,="" &buf2,="" 1="" );=""  =""  =""  =""  ="" while="" (="" c1="" !="0" &&="" c2="" !="0" )="" {=""  =""  =""  =""  =""  =""  ="" if="" (="" buf1="" !="buf2" )="" break;=""  =""  =""  =""  =""  =""  ="" c1="read(" lun1,="" &buf1,="" 1="" );=""  =""  =""  =""  =""  =""  ="" c2="read(" lun2,="" &buf2,="" 1="" );=""  =""  =""  =""  ="" }=""  =""  =""  =""  ="" close(="" lun1="" );=""  =""  =""  =""  ="" close(="" lun2="" );=""  =""  =""  =""  ="" if="" (="" c1="=" 0="" &&="" c2="=" 0="" )=""  =""  =""  =""  =""  =""  ="" printf(="" "files="" match.\n"="" );=""  =""  =""  =""  ="" else=""  =""  =""  =""  =""  =""  ="" printf(="" "files="" don't="" match="" %d="" %d.\n",="" c1,="" c2="" );=""  =""  ="" return="" 0;="" }="" i="" avoided="" using="" _read()="" and="" _close="" and="" _open,="" using="" the="" posix="" calls="" instead.="" since="" there="" is="" no="" standard,="" you="" choose="" your="" own="" poison,="" i="" guess.="" binary="" mode="" is="" used="" here="" to="" avoid="" "cooked="" mode"="" processing,="" which="" would="" not="" permit="" a="" genuine="" comparison="" of="" two="" files="" as="" identical.="" edit:="" take="" note="" that="" ari's="" approach="" won't="" work="" for="" several="" reasons,="" even="" assuming="" that="" fopen()="" was="" okay="" with="" you.="" for="" one,="" the="" files="" aren't="" opened="" in="" binary="" mode="" in="" his="" example="" (at="" the="" time="" i'm="" reading="" it="" now.)="" for="" another,="" he="" gets="" characters="" in="" the="" loop="" control="" and="" uses="" &&="" there.="" if="" the="" first="" file,="" let's="" say,="" is="" shorter="" than="" the="" second="" file="" his="" loop="" will="" exit="" prematurely="" and="" say="" that="" the="" files="" are="" the="" same,="" when="" they="" are="" not="" necessarily="" the="" same="" because="" the="" second="" file="" still="" may="" have="" more="" data="" in="" it.="" so,="" basically,="" it="" doesn't="" work="" as="" well="" as="" not="" meeting="" your="" request.="" he="" also="" doesn't="" compare="" against="" eof.="" and="" he="" doesn't="" properly="" declare="" the="" return="" type="" of="" fgetc(),="" instead="" putting="" the="" values="" into="" 'char'="" variables,="" when="" he="" should="" be="" using="" int.="" it's="" good="" for="" learning="" about="" commonly="" made="" mistakes,="" though.="" it's="" densely="" packed="" with=""></ 3 ) {             printf( "Requires two arguments for the two filenames.\n" );             return 0;         }         if ( (lun1= open( argv[1], O_RDONLY | O_BINARY )) == -1 ) {             printf( "Can't find %s to open.\n", argv[1] );             return 0;         }         if ( (lun2= open( argv[2], O_RDONLY | O_BINARY )) == -1 ) {             printf( "Can't find %s to open.\n", argv[2] );             close( lun1 );             return 0;         }         c1= read( lun1, &buf1, 1 );         c2= read( lun2, &buf2, 1 );         while ( c1 != 0 && c2 != 0 ) {             if ( buf1 != buf2 ) break;             c1= read( lun1, &buf1, 1 );             c2= read( lun2, &buf2, 1 );         }         close( lun1 );         close( lun2 );         if ( c1 == 0 && c2 == 0 )             printf( "Files match.\n" );         else             printf( "Files don't match %d %d.\n", c1, c2 );     return 0; }

I avoided using _read() and _close and _open, using the POSIX calls instead. Since there is no standard, you choose your own poison, I guess. Binary mode is used here to avoid "cooked mode" processing, which would not permit a genuine comparison of two files as identical.

EDIT: Take note that Ari's approach won't work for several reasons, even assuming that fopen() was okay with you. For one, the files aren't opened in binary mode in his example (at the time I'm reading it now.) For another, he gets characters in the loop control and uses && there. If the first file, let's say, is shorter than the second file his loop will exit prematurely and say that the files are the same, when they are not necessarily the same because the second file still may have more data in it. So, basically, it doesn't work as well as not meeting your request. He also doesn't compare against EOF. And he doesn't properly declare the return type of fgetc(), instead putting the values into 'char' variables, when he should be using int. It's good for learning about commonly made mistakes, though. It's densely packed with them.></fcntl.h></io.h></stdio.h>

ComentáriosComentários
Acho que a resposta não está correta ou que você gostaria de acrescentar mais
alguma informação? Envie o seu comentário abaixo..

Guest


HTML não é permitido!

Image Code

Digite os caracteres que aparecem na imagem por isso sabemos que você é humano!

Receber um email quando alguém acrescenta outro comentário a esta pergunta



Topo da página


Home  Terms
Copyright © Accelerated Ideas 2005-2024
All rights reserved