函数“write”的隐式声明;您指的是 ‘fwrite’ 吗?

implicit declaration of function ‘write’; did you mean ‘fwrite’?

我编译了一个简单的笔记程序示例,该程序使用书中的文件描述符,我遇到了一些与“写入”和“关闭”函数相关的编译器错误,以及使用的“strncat”功能。这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

void usage(char *prog_name, char *filename) {
        printf("Usage: %s <data to add to %s>\n", prog_name, filename);
        exit(0);
}

void fatal(char *);             // A function for fatar errors
void *ec_malloc(unsigned int);  // An error-checked malloc() wrapper

int main(int argc, char *argv[]) {
        int fd; // file descriptor
        char *buffer, *datafile;

        buffer = (char *) ec_malloc(100);
        datafile = (char *) ec_malloc(20);
        strcpy(datafile, "/tmp/notes");

        if (argc < 2)                     // If there aren't command-line arguments,
                usage(argv[0], datafile); // display usage message and exit.

        strcpy(buffer, argv[1]); // Copy into buffer.

        printf("[DEBUG] buffer   @ %p: \'%s\'\n", buffer, buffer);
        printf("[DEBUG] datafile @ %p: \'%s\'\n", datafile, datafile);

        strncat(buffer, "\n", 1); // Add a newline on the end.

        // Opening file
        fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
        if(fd == -1)
                fatal("in main() while opening file");
        printf("[DEBUG] file descriptor is %d\n", fd);
        //Writing data
        if(write(fd, buffer, strlen(buffer)) == -1)
                fatal ("in main() while writing buffer to file");
        //Closing file
        if(close(fd) == -1)
                fatal ("in main() while closing file");

        printf("Note has been saved.\n");
        free(buffer);
        free(datafile);
}

// A function to display and error message and then exit
void fatal(char *message) {
        char error_message[100];

        strcpy(error_message, "[!!] Fatal Error ");
        strncat(error_message, message, 83);
        perror(error_message);
        exit(-1);
}

// An error-checked malloc() wrapper function
void *ec_malloc(unsigned int size) {
        void *ptr;
        ptr = malloc(size);
        if(ptr == NULL) {
                fatal("in ec_malloc() on memory allocation");
                exit(-1);
        }
        return ptr;
}

这是编译器错误:

implenote.c: In function ‘main’:
simplenote.c:39:5: warning: implicit declaration of function ‘write’; did you mean ‘fwrite’? [-Wimplicit-function-declaration]
   39 |  if(write(fd, buffer, strlen(buffer)) == -1)
      |     ^~~~~
      |     fwrite
simplenote.c:42:5: warning: implicit declaration of function ‘close’; did you mean ‘pclose’? [-Wimplicit-function-declaration]
   42 |  if(close(fd) == -1)
      |     ^~~~~
      |     pclose
simplenote.c:31:2: warning: ‘strncat’ specified bound 1 equals source length [-Wstringop-overflow=]
   31 |  strncat(buffer, "\n", 1); // Add a newline on the end.

代码是从书上抄来的,没有任何改动,我想知道为什么会这样,我能做些什么来制作代码运行。请注意,这本书(Hacking: The Art of Exploitation, Second Edition)有点过时,于 2008 年发行。

要访问 Posix 低级文件接口,例如 openreadwriteclose,您应该包括 <unistd.h>.

但是请注意,您的程序似乎不需要如此低级的接口,您可以考虑使用 <stdio.h> 中声明的标准流创建输出文件,使用 fopenfputsfprintffclose.