【发布时间】:2015-04-04 10:46:49
【问题描述】:
我正在编写一个 unix cp 程序,但我不清楚是否检查 EOF。我的代码是:
int main(int argc, const char * argv[]) {
int in, out;
char buf[BUFFER_SIZE];
if (argc != 3)
cout << "Error: incorrect number of params" << endl;
if ((in = open(argv[1], O_RDONLY, 0666)) == -1)
cout << "Error: cannot open input file" << endl;
if ((out = open(argv[2], O_WRONLY | O_CREAT, 0666)) == -1)
cout << "Cannot create output file" << endl;
else
while ((read(in, buf, BUFFER_SIZE)) != -1)
write(out, buf, BUFFER_SIZE);
return 0;
}
它读写正常,但在写入输出文件时写入超过 EOF。所以我在文件末尾收到了几行乱码。我只是没有正确检查 EOF 吗?感谢您的意见。
【问题讨论】:
标签: unix operating-system system-calls