使用文件系统调用

使用文件系统调用

我正在尝试学习打开、写入和关闭文件的系统调用。我使用这个示例,结果是:

gcc: error trying to exec 'cc1plus': execvp: No such file or directory

这是我的程序:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main (int argc, char *argv[])
{
    int fd1;
    char buf[128];
    fd1 = open("Desktop/this.txt", O_WRONLY);
    if (fd1 == -1) {
        perror("File cannot be opened");
        return EXIT_FAILURE;
    }

    /* Enter the data to be written into the file */
    scanf("%127s", buf);

    write(fd1, buf, strlen(buf)); /* fd1 is the file descriptor, buf is the character array used to
 hold the data, strlen(buf) informs the function that the number of bytes equal to the length of the
 string in the buffer need to be copied */

    close(fdl);

    return 0;
}

答案1

我假设您使用的是 Ubuntu、Debian 或某些衍生版本。确保您的系统是最新的,然后安装 g++。

答案2

cc1plus是 C++ 编译器的一个组件。您正在尝试编译 C 程序。该gcc命令根据源文件的名称确定要调用哪个编译器: 的 C 编译器、 or.c的 C++ 编译器、 的 Pascal 编译器、 的汇编器等。.cc.C.p.s

看来您给您的程序起了一个表明 C++ 程序的名称。请注意 Linux 文件名区分大小写。 C 源文件必须具有扩展名.c(小写c),而不是.C. Unix 用户倾向于在文件名中使用大部分小写字母,特别是几乎所有常规文件扩展名都是小写的。因此请确保使用小写文件名。

相关内容