Home
|
Pesquisa
|
Você está em:
Programas
programas
>
cozinhar
programas
>
cozinhar
Categorias Populares
Celular
Computadores
Facebook
Minecraft
Video
Software
Jogos
Dicas Jogos
Relacionamentos
PayPal
Microsoft Office
Skype
iTunes
Windows 11
Youtube
Android
iPhone
iPad
Fotos
Programas
Google Chrome
Internet Explorer
Firefox
Mais
Antivirus
Viagem
Combat Arms
MSN
Hotmail
Celebridade
Tablet
Windows 8
TV Audio
Ebay
Fitness
Ciências
eBuddy
Gmail
Habbo
Windows 7
Tumblr
Beleza
Linkedin
Musica
PS3
Saude
Mac
Photoshop
Dieta
Instagram
Driver
Twitter
Club Penguin
XBox
Programacao C comparar dois arquivos?
comentários
Ver melhor comentário
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
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>
Inscrever
- receber um email se alguém adiciona novas respostas/comentários
Subscrever
Email*
Verificação*
Digite os caracteres que aparecem na imagem por isso sabemos que você é humano!
Comentá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..
Nome*
123
Comentários*
HTML não é permitido!
Verificação*
Digite os caracteres que aparecem na imagem por isso sabemos que você é humano!
Atualizações
Receber um email quando alguém acrescenta outro comentário a esta pergunta
Não, obrigado
Sim, por favor
Email
Mais como isso
programas
>
cozinhar
Outras Respostas Populares
Meu snapchat nao me deixa gravar e enviar videos?
Eu tenho gravado um CD, mas nao vai jogar no meu carro - o que e a correcao?
Eu quero apagar todos os arquivos pornograficos e pasta porno?
Como convencer minha mae a deixar-me fazer videos do youtube?
Alguem sabe de um bom software antivirus basico? Aquele que nao come um bando de CPU e memoria RAM?
Onde posso obter o software para controle de OVNIs?
Diga-me qualquer software que protege o meu pendrive de virus, worms, malware etc..?
Por favor ajude. nao e possivel leva-lo mais?
Eu encontrei um 'virus' no meu pendrive, software antivirus removido-lo. E seguro usar?
Como parar os videos que eu nao assisto aparecendo no meu historico de visualizacao (YouTube)?
Google pede um CAPTCHA mas nao funciona? Por favor ajude?
O dano e desfragmentacao vai meu computador fazer? Memoria virtual insuficiente?
Problema s3 galaxy Samsung com conexao para casa Wi-Fi?
O que e o software de protecao muito melhor para um PC?
Por que os Macs sao melhores do que PCs?!?
Usuários da rede social
Estas pessoas adicionaram comentários ou respostas recentemente
Topo da página
Home
Terms
Copyright © Accelerated Ideas 2005-2024
All rights reserved
Home
Software Gratis
Respostas
Notícias
Minecraft Wiki
Musicas Gratis
GTA 5
Instagram