c - 如何检查文件在 Unix C 中是否相同?

标签 c file unix

如何使用 Unix C 检查一个文件是否与另一个文件相同(具有相同的内容)?我的意思是,当我不能使用 fopen、fread、fclose 而只能使用 open、read、close 时?我对显示如何仅在 Unix C 中执行此操作的答案感兴趣。

我写了一个程序,可以将一个文件复制到另一个文件,但不知道如何检查它们是否相同:/:

#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;
}

这是我尝试过的:

#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;
}

但它表明文件是相同的,但它们不是 :(

最佳答案

你只需要同时读取两个缓冲区。例如(同时考虑处理错误),根本不使用 C 标准库:

#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;
}

注意(感谢user4815162342):这段代码并不完全正确。实际上,如果 read 返回的读取字节数小于请求的字节数,则不是错误。但是,为了缩短此代码,我没有包含此管理。

关于c - 如何检查文件在 Unix C 中是否相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12769781/

相关文章:

c - 使用 VS Code、Makefile 和自定义 bash 脚本在 C 语言中调试头文件

c - 快速找到标题中的定义

c - 如何在 Rust 中获取给定 CPU 寄存器的偏移量

unix - 将输入与输出连接到 svn list 命令,然后将其传递给 grep

Linux Shell 脚本什么目录名和?方法?

c++ - 以特定偏移量从 C++ 中的文本文件中读取

javascript - 在 chrome 开发工具中的文件 :///url - evidence it is being created but not showing in document. cookie 或 cookie 列表上创建 cookie

objective-c - 检查文件是否为空

file - VS 2012 : Scroll Solution Explorer to current file

shell - scp 中的正则表达式/通配符