繁体   English   中英

函数'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");
        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.

代码是从书中复制的,没有做任何改动,我想知道为什么会发生这种情况以及我可以做些什么来使代码运行。 请注意,这本书(黑客:剥削的艺术,第二版)有点过时,于 2008 年发行。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM