我正在用 C 编写一个十六进制转储程序。我知道那里有大量的十六进制转储程序,但我想写一个来体验一下。我已经在 Windows 上用 CodeBlocks 编写了该程序,但我似乎无法让它运行。
我正在读取大约 137,000 字节的测试程序,但该程序在 417 字节处停止。现在,当我在 Linux 上编译代码时(因为它只是一个控制台应用程序并且正在使用标准 C 库),它可以完美运行,并返回文件中正确的字节数。有谁知道为什么 read() 不能在 Windows 上运行,但在 Linux 上运行良好?
下面是我如何读取文件的示例。
int main(int argc, char **argv)
{
if (argc != 2) { return 1; }
int fd = open(argv[1], O_RDONLY);
if (fd == -1) { return 1; }
unsigned char buffer[8];
unsigned int bytes = 0;
unsigned int total_bytes = 0;
while ((bytes = read(fd, buffer, sizeof(unsigned char) * 8)) > 0) {
...
total_bytes += bytes;
}
printf("Total Bytes: %d\n", total_bytes);
return 0;
}
最佳答案
我终于在这篇文章中找到了答案。不过,他们遇到了 stdin 的问题。显然替换字符 (1A) 与 Windows 中的 CTRL+Z 相同,因此在读取该字符时强行关闭我的程序。
关于c - 程序不读取整个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56141449/