读取通用文件
我正在制作一个程序,从标准输入读取文件,对其执行某些操作并将其发送到标准输出。
就目前情况而言,我的程序中有一行:
while((c = getchar()) != EOF){
其中 c
是一个 int。
然而问题是我想在 ELF 可执行文件上使用这个程序。看来可执行文件内必须有代表 ASCII 文件 EOF 的字节,这会导致它被截断(如果我错了,请纠正我 - 这只是我的假设)。
执行此操作的有效一般方法是什么?我可以挖掘 ELF 格式的文档,然后检查最后的内容。这很有用,但我认为如果我仍然可以将此程序应用于任何类型的文件,那就更好了。
I'm making a program that reads in a file from stdin, does something to it and sends it to stdout.
As it stands, I have a line in my program:
while((c = getchar()) != EOF){
where c
is an int.
However the problem is I want to use this program on ELF executables. And it appears that there must be the byte that represents EOF for ascii files inside the executable, which results in it being truncated (correct me if I'm wrong here - this is just my hypothesis).
What is an effective general way to go about doing this? I could dig up documents on the ELF format and then just check for whatever comes at the end. That would be useful, but I think it would be better if I could still apply this program to any kind of file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你会没事的 -
EOF
常量不包含有效的 ASCII 值(通常为-1
)。例如,下面是我系统上的
stdio.h
的摘录:You'll be fine - the
EOF
constant doesn't contain a valid ASCII value (it's typically-1
).For example, below is an excerpt from
stdio.h
on my system:您可能想要降低一点级别并使用 open()、close() 和 read() 等系统函数,这样您就可以对输入执行您喜欢的操作,因为它将存储在您自己的缓冲区中。
You might want to go a bit lower level and use the system functions like open(), close() and read(), this way you can do what you like with the input as it will get stored in your own buffer.
你做得正确。
EOF 不是字符。
c
不可能用 EOF 来表示流中的任何字节。如果/当c
确实包含EOF时,该特定值并非源自文件本身,而是源自底层库/操作系统。 EOF 是出现问题的信号。确保
c
是一个int
,但是哦...你可能想从你控制的流中读取数据。如果没有代码可以执行其他操作,
stdin
会受到“文本翻译”的影响,这在读取二进制数据时可能并不理想。You are doing it correctly.
EOF is not a character. There is no way
c
will have EOF to represent any byte in the stream. If / whenc
indeed contains EOF, that particular value did not originate from the file itself, but from the underlying library / OS. EOF is a signal that something went wrong.Make sure
c
is anint
thoughOh ... and you might want to read from a stream under your control. In the absence of code to do otherwise,
stdin
is subject to "text translation" which might not be desirable when reading binary data.从 getchar(3) 手册页:
这意味着,通过 getchar 读取的字符值永远不能等于有符号整数 -1。这个小程序解释了这一点:
From the getchar(3) man page:
This means, a character value read via getchar, can never be equal to an signed integer of -1. This little program explains it: