【问题标题】:systemcalls.h no such file or directory foundsystemcalls.h 没有找到这样的文件或目录
【发布时间】:2014-09-13 22:15:03
【问题描述】:

我正在阅读 K&R 的 C 编程,我刚刚开始阅读最后一章:UNIX 系统接口。我遇到了进行系统调用的文件复制代码。首先,我在代码块窗口中编译了该代码,我得到了一个找不到目录/文件的错误,然后我想我应该在 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


    【解决方案1】:
    #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
    

    【讨论】:

    • 这本书错了,为什么他们使用 syscalls.h ?迷茫学什么不学什么
    • 也许这本书引用了作者创建的自定义 heder,因为 incude 在引号中,“syscalls.h”,而引用系统头通常在括​​号中,
    • 这是一本相当古老的书(最近一版出版于 1988 年)。有些信息可能已经过时了。
    • 错误:“BUFSIZE”未声明(在此函数中首次使用)
    • BUFSIZEstdio.h 中可用
    【解决方案2】:

    "syscalls.h" 更改为&lt;sys/syscall.h&gt;。这是 Linux 中的正确标头。

    添加#include &lt;stdio.h&gt;得到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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-22
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 2016-09-17
      • 2021-06-24
      • 2015-02-20
      相关资源
      最近更新 更多