systemcalls.h找不到这样的文件或目录

问题描述 投票:3回答:2

我正在阅读K&R的C程序设计,而我刚刚开始了最后一章:UNIX系统接口。我遇到了一个文件复制代码,该文件进行了系统调用。首先,我在codeblocks窗口中编译了该代码,但出现了一个目录/文件未找到的错误,然后我认为我应该在Linux中编译该代码。但是我比之后得到了同样的错误。

我阅读了关于stackoverflow的其他一些问题:sudo apt-get更新然后再次安装linux头文件

阅读了使用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;
}
c unix system-calls
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;
}

EDIT:可以使用unistd.h。也修复了错字!

输出:

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

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.