How to check if files match in Unix C?

How to check if one file is the same (has the same content) as another file using Unix C? I mean, when I can not use fopen, fread, fclose , but just open, read, close ? I'm interested in answers that show how to do this ONLY in Unix C.

I wrote a program that copies one file to another, but has no idea how to check if they match: /:

 #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { const char *in_filename = "in.txt", *out_filename = "out.txt"; int in_fd, out_fd, bytes_read, bytes_written; int buffsize = 512; char buffer[512]; int success = 0; in_fd = open(in_filename, O_RDONLY); if (in_fd == -1) return -1; out_fd = open(out_filename, O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR); if (out_fd == -1) return -1; for(;;) { bytes_read = read(in_fd, buffer, buffsize); if (bytes_read > 0) { bytes_written = write(out_fd, buffer, bytes_read); if(bytes_written < 0) return -1; } else { if (bytes_read == 0) { if (close(in_fd) < 0) return -1; if (close(out_fd) < 0) return -1; success = 1; break; } else if (bytes_read == -1) { break; return -1; } } } if(success) fprintf(stdout, "%s", "Success!\n"); return 0; } 

Here is what I tried:

 #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { const char *in_filename = "in.txt", *out_filename = "out.txt"; int in_fd, out_fd, bytes_read_in, bytes_read_out; int buffsize = 512; char in_buffer[512], out_buffer[512]; int the_same = 0; in_fd = open(in_filename, O_RDONLY); if (in_fd == -1) return -1; out_fd = open(out_filename, O_RDONLY); if (out_fd == -1) return -1; for(;;) { bytes_read_in = read(in_fd, in_buffer, buffsize); if (bytes_read_in > 0) { bytes_read_out = read(out_fd, out_buffer, buffsize); if(bytes_read_out > 0) { int i = 0; for(i=0; i<buffsize; i++) { if(in_buffer[i] != out_buffer[i]) the_same = 0; } the_same = 1; } } else { if (bytes_read_in == 0) { if (close(in_fd) < 0) return -1; if (close(out_fd) < 0) return -1; break; } else if (bytes_read_in == -1) { break; return -1; } } } if(the_same) fprintf(stdout, "%s", "Files are the same!\n"); return 0; } 

but it shows that the files are the same, and theyre not: (

+4
source share
2 answers

You just need to read two buffers at the same time. For example (think to handle errors), without using the standard C library:

 #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #define BUFFER_SIZE 1024 static int bufcmp(const void *p, const void *q, size_t n) { const char *p1 = p; const char *p2 = q; while (n-- > 0) { if (*p1++ != *p2++) return 0; } return 1; } int main(int argc, char *argv[]) { int fd1 = open(argv[1], O_RDONLY); int fd2 = open(argv[2], O_RDONLY); int same = 1; for (;;) { char buf1[BUFFER_SIZE], buf2[BUFFER_SIZE]; ssize_t n1 = read(fd1, buf1, BUFFER_SIZE); ssize_t n2 = read(fd2, buf2, BUFFER_SIZE); if (n1 < n2) { same = 0; break; } else if (n1 == 0) { break; } else if (bufcmp(buf1, buf2, n1) == 0) { same = 0; break; } } if (same) write(STDOUT_FILENO, "Same content.\n", 14); close(fd1); close(fd2); return 0; } 

NB (thanks user4815162342 ): This code is not quite right. Indeed, this is not an error if the number of bytes read returned by read is less than the number of bytes requested. However, to shorten this code, I did not enable this control.

+3
source

What is the problem of using two buffers, one for each file, to read the same number of bytes in each, and then to compare the contents of the buffer with memcmp ?

+1
source

Source: https://habr.com/ru/post/1438262/


All Articles