read() 只从文件中读取几个字节

发布于 2024-12-25 02:09:36 字数 1650 浏览 1 评论 0原文

我想使用 read() 函数读取文件的内容。我尝试了以下操作:

#define BUFFER_LENGTH (1024)

char buffer[BUFFER_LENGTH];

// The first version of the question had a typo:
// void read_file(const char filename)
// This would produce a compiler warning.
void read_file(const char *filename)
{
    ssize_t read_bytes = 0;

    // The first version had the mode in hex instead of octal.
    //
    //     int fd_in = open(filename, O_RDONLY, 0x00644);
    //
    // This does not cause problems here but it is wrong.
    // The mode is now octal (even if it is not needed).
    int fd_in = open(filename, O_RDONLY, 0644);
    if (fd_in == -1)
    {
        return;
    }

    do
    {
        read_bytes = read(fd_in, buffer, (size_t) BUFFER_LENGTH);
        printf("Read %d bytes\n", read_bytes);

        // End of file or error.
        if (read_bytes <= 0)
        {
            break;
        }
    } while (1);

    close(fd_in);
}

我在 Windows 7 系统上使用“gcc (GCC) 3.4.2 (mingw-special)”。

我得到的奇怪行为是,并非所有内容都被阅读。例如,我有 一个文件

05.01.2012  12:28            15.838 hello.exe

,当我尝试读取它时,我得到:

Read 216 bytes
Read 0 bytes

据我所知 read() 应该继续读取,直到到达文件末尾。虽然确实 第二次调用时它会报告文件结尾(0)吗?

也许我错过了一些明显的东西,但我看不到它。我已阅读 本文档这个文档一遍又一遍,我不能找出我做错了什么。有人有任何线索吗?

编辑

感谢您的提示!这是问题中的拼写错误(我已更正)。源码中是正确的 代码。

I wanted to read the content of a file using the read() function. I tried the following:

#define BUFFER_LENGTH (1024)

char buffer[BUFFER_LENGTH];

// The first version of the question had a typo:
// void read_file(const char filename)
// This would produce a compiler warning.
void read_file(const char *filename)
{
    ssize_t read_bytes = 0;

    // The first version had the mode in hex instead of octal.
    //
    //     int fd_in = open(filename, O_RDONLY, 0x00644);
    //
    // This does not cause problems here but it is wrong.
    // The mode is now octal (even if it is not needed).
    int fd_in = open(filename, O_RDONLY, 0644);
    if (fd_in == -1)
    {
        return;
    }

    do
    {
        read_bytes = read(fd_in, buffer, (size_t) BUFFER_LENGTH);
        printf("Read %d bytes\n", read_bytes);

        // End of file or error.
        if (read_bytes <= 0)
        {
            break;
        }
    } while (1);

    close(fd_in);
}

I am using 'gcc (GCC) 3.4.2 (mingw-special)' on a Windows 7 system.

The strange behaviour I get is that not all the content is read. For example, I have
a file

05.01.2012  12:28            15.838 hello.exe

and when I try to read it I get:

Read 216 bytes
Read 0 bytes

As far as I know read() should keep reading until it reaches the end of the file. While does
it report an end of file (0) the second time it is called?

Maybe I am missing something obvious but I cannot see it. I have read this document and this document over and over again and I cannot find what I am doing wrong. Does anyone have any clue?

EDIT

Thanks for the hint! It is a typo in the question (I have corrected it). It is correct in the source
code.

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

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

发布评论

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

评论(4

找回味觉 2025-01-01 02:09:36

我怀疑字节 217 是 EOF (26, 0x1A) - 在 Windows 中文件可以以“文本”或“二进制”模式打开。在文本模式下,0x1A 被解释为 EOF。

您需要查看您的打开模式 - O_BINARY。在 PHP 中,这就是为什么您必须使用“rb”(READ BINARY)模式而不是“R”(“R”默认为“READ TEXT”)模式打开。

http://www.mingw.org/wiki/FAQ 表示该标志是 O_BINARY (靠近页面底部),所以你需要

int fd_in = open(filename, O_RDONLY | O_BINARY, 0644);

http://cygwin.com/faq.html 第 5.3 段告诉你如何在 cygwin 中处理这个问题

I suspect byte 217 to be EOF (26, 0x1A) - in Windows files can be opened in "text" or "binary" mode. In text mode, a 0x1A is interpreted as EOF.

You would need to look at your open mode - O_BINARY. In PHP this is why you must fopen with mode "rb" (READ BINARY) and not "R" ("R" which defaults to READ TEXT).

http://www.mingw.org/wiki/FAQ says the flag is O_BINARY (near bottom of page), so you'd need

int fd_in = open(filename, O_RDONLY | O_BINARY, 0644);

http://cygwin.com/faq.html paragraph 5.3 tells you how to handle this in cygwin

风轻花落早 2025-01-01 02:09:36

void read_file(const char filename)

然后是:

int fd_in = open(filename, O_RDONLY, 0x00644);

不要忽略编译器警告。我很惊讶这不仅仅是崩溃。

void read_file(const char filename)

and then later:

int fd_in = open(filename, O_RDONLY, 0x00644);

Don't ignore compiler warnings. I am surprised this didn't just crash.

幽梦紫曦~ 2025-01-01 02:09:36

您可能想尝试使用 O_RDONLY | O_BINARY 或 O_RDONLY | O_NOTRANS 在公开调用中。如果不指定 O_BINARY 或 O_NOTRANS,则可以以文本模式打开文件,并且读取将在第一次遇到 EOF 字符时停止。

You may want to try using O_RDONLY | O_BINARY or O_RDONLY | O_NOTRANS in the open call. By not specifying O_BINARY or O_NOTRANS, the file may be opened in text mode and the read will stop at the first encounter of the EOF character.

在你怀里撒娇 2025-01-01 02:09:36

我在我的机器上尝试了你的代码:

  • Windows 7,
  • Cygwin 今天的最新版本,
  • gcc (GCC) 3.4.4(cygming 特殊,gdc 0.12,使用 dmd 0.125))

,它对于我的机器上的示例文件运行良好。我读取的文件是 C:\Windows\System32 中的 cmd.exe,我将 read_file 函数的总字节数与实际值进行了比较磁盘上的文件大小与它们匹配。

这表明以下两件事之一:

  • 您正在打开的文件有一些特殊之处。也许它处于奇怪的锁定状态,并且您在中途遇到一些错误(但从未听说过),或者文件可能在磁盘上已损坏(其他程序可以访问它吗?尝试将其复制到另一个文件夹)
  • 您的代码中有一些内容不在导致问题的问题中

I tried your code on my machine:

  • Windows 7,
  • Cygwin latest version as of today,
  • gcc (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125))

and it worked fine for a sample file on my machine. The file I read was cmd.exe in C:\Windows\System32 and I compared the total byte count from your read_file function with the actual file size on disk and they matched.

This suggests one of two things:

  • There is something special with the file that you're opening. Maybe it's in a weird locked state and you get some error halfway through (never heard of that though) or maybe the file is corrupt on disk (can other programs access it? Try copying it to another folder)
  • There is something in your code that isn't in the question that is causing the problem
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文