gpt4 book ai didi

c - 替换 Linux 内置的 cmp 实用程序

转载 作者:太空宇宙 更新时间:2023-11-04 03:45:41 27 4
gpt4 key购买 nike

Linux 的 cmp 命令逐字节比较两个文件,并返回第一个差异(如果现在存在差异,则不返回任何内容)。

在 C 中可以做些什么或其他什么来做到这一点?是否会像比较 getcharfscanf 的两次调用一样简单,一旦发现差异就立即中断 break

最佳答案

Linux's cmp command compares two files, byte-by-byte ...

经过一些优化:

Get the optimal block size of the files.
Find the rough position of the first difference by reading words, not bytes.

Would it be as simple as comparing two calls of getchar or maybe fscanf and breaking as soon as a difference is found?

它有一些额外的东西:

   -i, --ignore-initial=SKIP
skip first SKIP bytes of both inputs

-i, --ignore-initial=SKIP1:SKIP2
skip first SKIP1 bytes of FILE1 and first SKIP2 bytes of FILE2

-n, --bytes=LIMIT
compare at most LIMIT bytes

你可以看看 cmp.c [ source code ]

关于c - 替换 Linux 内置的 cmp 实用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24710618/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com