read() 只从文件中读取几个字节
我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我怀疑字节 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 (靠近页面底部),所以你需要
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
http://cygwin.com/faq.html paragraph 5.3 tells you how to handle this in cygwin
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.
您可能想尝试使用 O_RDONLY | O_BINARY 或
O_RDONLY | O_NOTRANS
在公开调用中。如果不指定 O_BINARY 或 O_NOTRANS,则可以以文本模式打开文件,并且读取将在第一次遇到 EOF 字符时停止。You may want to try using
O_RDONLY | O_BINARY
orO_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.我在我的机器上尝试了你的代码:
,它对于我的机器上的示例文件运行良好。我读取的文件是
C:\Windows\System32
中的cmd.exe
,我将read_file
函数的总字节数与实际值进行了比较磁盘上的文件大小与它们匹配。这表明以下两件事之一:
I tried your code on my machine:
and it worked fine for a sample file on my machine. The file I read was
cmd.exe
inC:\Windows\System32
and I compared the total byte count from yourread_file
function with the actual file size on disk and they matched.This suggests one of two things: