如何在c中读取二进制文件? (视频、图像或文字)

发布于 2024-08-22 12:58:18 字数 753 浏览 11 评论 0原文

我正在尝试将文件从指定的库复制到当前目录。我可以完美地复制文本文件。任何其他文件都会损坏。程序提前检测到 feof。

#include <stdio.h>

int BUFFER_SIZE = 1024;
FILE *source;
FILE *destination;
int n;
int count = 0;
int written = 0;

int main() {
    unsigned char buffer[BUFFER_SIZE];

    source = fopen("./library/rfc1350.txt", "r");

    if (source) {
        destination = fopen("rfc1350.txt", "w");

        while (!feof(source)) {
            n = fread(buffer, 1, BUFFER_SIZE, source);
            count += n;
            printf("n = %d\n", n);
            fwrite(buffer, 1, n, destination);
        }
        printf("%d bytes read from library.\n", count);
    } else {
        printf("fail\n");
    }

    fclose(source);
    fclose(destination);

    return 0;
}

I am trying to copy a file from a specified library to the current directory. I can copy text files perfectly. Any other files become corrupt. The program detects a feof before it should.

#include <stdio.h>

int BUFFER_SIZE = 1024;
FILE *source;
FILE *destination;
int n;
int count = 0;
int written = 0;

int main() {
    unsigned char buffer[BUFFER_SIZE];

    source = fopen("./library/rfc1350.txt", "r");

    if (source) {
        destination = fopen("rfc1350.txt", "w");

        while (!feof(source)) {
            n = fread(buffer, 1, BUFFER_SIZE, source);
            count += n;
            printf("n = %d\n", n);
            fwrite(buffer, 1, n, destination);
        }
        printf("%d bytes read from library.\n", count);
    } else {
        printf("fail\n");
    }

    fclose(source);
    fclose(destination);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟燃烟灭 2024-08-29 12:58:18

您使用的是 Windows 机器吗?尝试将“b”添加到调用 fopen 的模式字符串中。

来自 man fopen(3):

模式字符串还可以包含字母“b”作为最后一个字符或作为上述任何两个字符字符串中的字符之间的字符。这严格是为了兼容C89,没有任何作用;在所有符合 POSIX 的系统(包括 Linux)上,“b”都会被忽略。 (其他系统可能会处理文本文件和二进制文件
文件不同,如果您执行 I/O,添加“b”可能是个好主意
到一个二进制文件并期望你的程序可以移植到非 Unix
环境。)

Are you on a Windows machine? Try adding "b" to the mode strings in the calls to fopen.

From man fopen(3):

The mode string can also include the letter 'b' either as a last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with C89 and has no effect; the 'b' is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary
files differently, and adding the 'b' may be a good idea if you do I/O
to a binary file and expect that your program may be ported to non-Unix
environments.)

一抹苦笑 2024-08-29 12:58:18

您需要为 fopen 指定 "b" 选项:

source = fopen("./library/rfc1350.txt", "rb");
...
destination = fopen("rfc1350.txt", "wb");

如果没有它,文件将以文本 ("t") 模式打开,并且这会导致行尾字符的翻译。

You need to specify the "b" option to fopen:

source = fopen("./library/rfc1350.txt", "rb");
...
destination = fopen("rfc1350.txt", "wb");

Without it, the file is opened in text ("t") mode, and this results in translation of end-of-line characters.

毁我热情 2024-08-29 12:58:18

您需要以二进制格式而不是文本格式打开文件。在调用 fopen 时,使用 "rb""wb" 而不是 "r"“w” 分别。

You need to open the files in binary format rather than text format. In your calls to fopen, use "rb" and "wb" rather than "r" and "w" respectively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文