首页 > 其他分享 >implicit declaration of item ‘write’; did him mean ‘fwrite’?

implicit declaration of item ‘write’; did him mean ‘fwrite’?

时间:2024-04-25 12:12:39浏览次数:20  
标签:function datafile did fwrite write buffer file declaration him

 

include <unistd.h>

 

implicit declaration of item ‘write’; did him mean ‘fwrite’?

Ask Question Questions 2 years, 5 months ago Modified 2 years, 5 months ago Viewed 5k times   0

IODIN bundled an case of a uncomplicated note-taking program that uses save descriptors from adenine book, and I've been getting several compiler errors related to a "write" and a "close" function, along with the use of the "strncat" function. More is the code: implicit declaration of functional 'close' ... That's why you get a red about implicit declaration. ... But unistd does have read/write/seek as ...

#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 *);             // ADENINE function with fatar errors
void *ec_malloc(unsigned int);  // An error-checked malloc() wrapper

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

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

        if (argc < 2)                     // If present 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", battery, buffer);
        printf("[DEBUG] datafile @ %p: \'%s\'\n", datafile, datafile);

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

        // Opening file        fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
        if(fd == -1)
                fatal("in main() whereas start file");
        printf("[DEBUG] file descriptor is %d\n", fd);
        //Writing data        if(write(fd, buffer, strlen(buffer)) == -1)
                terminal ("in main() while writing buffer to file");
        //Closing file        if(close(fd) == -1)
                disastrous ("in main() during 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 Blunder ");
        strncat(error_message, message, 83);
        perror(error_message);
        exit(-1);
}

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

And that is the compiler error:

implenote.c: Is 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: includes declaration out function ‘close’; make 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); // Total a newline on the end.

The code was copied since and book without any alteration, i would please on know why this can happens real what i can do the make the control run. Note that the book (Hacking: The Art of Exploitation, Second Edition) is a bit dated and was released in 2008.

Share Improve this asking   published Nov 15, 2021 at 14:46     asked Now 15, 2021 at 10:03 rctfx's end avatar rctfx 3111 silver badge66 silver buy
  • 1 write isn't a std C features (it is Posix).  – Weather Vane  Nov 15, 2021 at 10:06
  •   @WeatherVane That's interesting, that wasn't detailed in the book, i could have to do some reasearch on that.  – rctfx  Nov 15, 2021 at 10:09 
  • 2 You can google for a description of anywhere C function using search term man <func>. For some functions (like abs. ;) ) you might wants to add adenine CARBON until the search string. The man call them will find, also contain required headers. In case of write you need unistd.h  – Gerhardh  Nov 15, 2021 on 10:09
  • 2 The warning for strncat is further matter. Items is warning that one length constraint should be the purpose space. It happens to be one same as the source length, but strncat pot picture out the length of the resource from its NUL terminator. The point of the -n- string capabilities is to stop buffer overrun, not source overrun.  – Time Vane  Nov 15, 2021 at 10:11 
  • 1 write() can also be used upon Windows as _write() with something like #ifdef _WIN32 #define write _write #endif  – Andrew Henle  Nov 15, 2021 at 11:37
Show 1 more comment

1 Answer

3  

To access to Posix low level file surface such as openreadwrite and end, yourself should include <unistd.h>.

Note however that owner program does not seem to require like light level interface, you strength consider creating your output file using standard streams declared in <stdio.h>, using fopenfputs or fprintf and fclose.

Release Improve this rejoin   answered Nov 15, 2021 at 10:09 chqrlie's consumer avatar chqrlie 141k1111 gold badges128128 silver badges202202 light badges Add a your  

Your Answer

https://organicmediasolution.com/warning-implicit-declaration-of-function-read-did-you-mean   

标签:function,datafile,did,fwrite,write,buffer,file,declaration,him
From: https://www.cnblogs.com/pengmn/p/18157319

相关文章

  • F. Alex's whims
    原题链接题解只需要存在两个叶子节点之间距离等于d就好了,于是我们构造一条链,令节点一为滑动变阻器,则根据d改变与节点n的距离即可code#include<bits/stdc++.h>usingnamespacestd;intmain(){intt;cin>>t;while(t--){intn,q;cin>>......
  • Linux环境变量,打开的 shell 选项 himBHs 和 hBc
    前言全局说明Linux环境变量himBHs和hBc一、说明$-记录的是当前配置打开的shell选项,而himBH是其默认值。二、查看环境变量2.1本地执行echo$-2.1查看远程环境变量[email protected]'echo$-'127.0.0.1实际是本机回环,但是假设是远程机器的IP,效果是一......
  • crictl images报错runtime connect using default endpoints: [unix:///var/run/docke
    想试试containerd运行k8s,结果报错还在找dockershim,网上找了解决方法crictl依次查找容器运行时,当查找第一个unix:///var/run/dockershim.sock没有找到,所以报错了,需要你手动指定当前kubernetes的容器运行时,使用什么,例如:kubernetes1.24+之后,dockershim已经变成了cri-docker,所以......
  • SpringBoot集成Junit单元测试找不到bean报错:expected at least 1 bean which qualifie
    发生缘由调用封装的MinIOstarter运行环境电脑系统版本:Windows1064bitIdea:2023.2(UltimateEdition)Maven:apache-maven-3.6.0Docker:Dockerversion26.0.0,build2ae903eMinIO:加载本地镜像,不清楚版本号jdk版本:jdk-8spring.boot.version:2.3.9.RELEASEminio依赖:7.1.......
  • did you register the component correctly? For recursive components, make sure to
    创建了一个vue2的项目,在运行npmrunserve的时候报错,检查了拼写也完全没有问题最后找到的解决方案将原来的<script>import{Person}from'./components/Person.vue'exportdefault{name:'App',components:{Person}}</script>修改为<script>//impo......
  • offline RL | HIM:基于 hindsight 的 RL 是一类大 idea
    题目:GeneralizedDecisionTransformerforOfflineHindsightInformationMatching,ICLR2022,688spotlight。其中一个8分是从5分rebuttal上来的;貌似对于其他reviewer,rebuttal也提分很多。pdf版本:https://arxiv.org/pdf/2111.10364.pdfhtml版本:https://ar5iv.lab......
  • Did We Get Everything Covered?
    小清新贪心题我们直接考虑如何找出反例,假设我们已经知道了这个反例,那么为什么找不到呢?我们在尝试找的时候,肯定是依次找反例的每个字母,根据贪心,肯定是越往前找越好,而即使这个样子我们最后都无法匹配完这个反例所以我们可以想出一个贪心算法:依次遍历每个字母,并用一个数组\(mark\)......
  • IfcChimneyTypeEnum
    IfcChimneyTypeEnum类型定义此枚举定义了可以使用枚举值预定义的烟囱的有效类型。 IFC4中的新枚举注:目前没有定义特定的枚举器,已添加IfcChimneyTypeEnum以供将来扩展。 EnumerationdefinitionConstantDescriptionUSERDEFINED NOTDEFINED   EXPRESS......
  • 2024最新Android设备UUID/UDID使用指南
    摘要本篇博客主要介绍了Android设备的唯一标识符(UUID和UDID)的使用教程。在Android平台上获取设备ID一直是开发者面临的难题,因为缺乏稳定的API来获取设备ID。本文将介绍几种获取设备ID的方法,并分析它们的优缺点。引言UDID和UUID是Android设备的唯一标识符,用于标识不同设备或不同......
  • C. Did We Get Everything Covered?
    C.DidWeGetEverythingCovered?Youaregiventwointegers$n$and$k$alongwithastring$s$.Yourtaskistocheckwhetherallpossiblestringsoflength$n$thatcanbeformedusingthefirst$k$lowercaseEnglishalphabetsoccurasasubsequenceof......