systemcalls.h文件不存在

3
我正在阅读K&R编写的C编程书籍,现在开始最后一章:UNIX系统接口。我遇到了一个文件复制代码,其中涉及到一个系统调用。首先,我在Codeblocks Windows中编译这个代码时出现了一个错误,说找不到dir/file,于是我想在Linux中编译这个代码。但是此后我仍然得到相同的错误信息。
我在stackoverflow上阅读了一些其他的问题,并执行了以下操作: sudo apt-get update 重新安装了linux header 我还在某个地方看到使用syscall.h,但是在那里BUFSIZ没有定义,我不认为这本书是错误的。
#include "syscalls.h"

int main()
{
    char buf[BUFSIZ];
    int n;
    while((n = read(0, buf, BUFSIZ)) > 0)
    write(1, buf, n);
    return 0;
}
2个回答

4
#include <unistd.h>
#include<stdio.h>
main()
{
char buf[BUFSIZ];
int n;
while((n = read(0,buf,BUFSIZ))>0)
     write(1,buf,n); //Output to the Console
return 0;
}

编辑:可以使用unistd.h。已修正拼写错误!

输出:

myunix:/u/mahesh> echo "Hi\nWorld" | a.out
Hi
World

2
也许这本书指的是作者创建的自定义头文件,因为引用使用了双引号 "syscalls.h",而引用系统头文件通常会使用尖括号 <syscalls.h>。 - nos
2
这是一本相当古老的书(最近的版本出版于1988年)。其中一些信息可能已经过时了。 - McLovin
错误:在此函数中未声明“BUFSIZE”(第一次使用) - user3858912
1
BUFSIZE 可在 stdio.h 中使用。 - Maheswaran Ravisankar
@MaheswaranRavisankar 我已经包含了<stdio.h>。 - user3858912
显示剩余3条评论

1

"syscalls.h"更改为<sys/syscall.h>,这是在Linux中正确的头文件。

添加#include <stdio.h>以获取BUFSIZE

您的代码中还有一些拼写错误:
- 在while语句中将BIFSIZE更改为BUFSIZE。现在它将可以编译。
- 然而,您还忘记在循环中分配n。更改为n = read(

最终代码应为:

#include <stdio.h>
#include <sys/syscall.h>
main()
{
    char buf[BUFSIZ];
    int n;
    while((n = read(0,buf,BUFSIZ))>0)
        write(1,buf,n);
    return 0;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接