Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

c - Implicit declaration of function ‘close'

I want to close a file associated with a handle, but I'm getting a warning from the compiler:

main.c:96:2: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]

And this is my source code:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
...
int handle;
...
handle = open(path, flags, mode);
...
close(handle);

Why am I getting this warning and how can I solve it?

This is the complete source code:

main.c

#include "header.h"

// Prototypes
void menu(char choix);
void creer();
void lire();
int ouvrir(char *path, int flags, mode_t mode);

int main(int argc, char **argv)
{

    char choix;
    int c;
    printf(PROGRAME_NAME, CYAN_BOLD,RESETCOLOR, CYAN_BOLD_BG, RESETCOLOR, CYAN_BOLD, RESETCOLOR);
    do{
        //printf("e[1;1He[2J");
        printf("

%sMenu :%s
", RED_BOLD, RESETCOLOR); 
        printf("(%sC%s)réer un fichier
", RED_BOLD, RESETCOLOR);
        printf("(%sL%s)ire un fichier
", RED_BOLD, RESETCOLOR);
        printf("(%sE%s)crire sur un fichier
", RED_BOLD, RESETCOLOR);
        printf("(%sS%s)upprimer un fichier
",RED_BOLD, RESETCOLOR);
        printf("(%sQ%s)uitter
",RED_BOLD, RESETCOLOR);
        do{
            printf("
%sVotre choix :%s ",GREEN_BOLD,RESETCOLOR);
            do {
                c = getchar();
                choix = tolower(c);
            } while (c == '
');
        }while((choix != 'c') && (choix != 'l') && (choix != 'e') && (choix != 's') && (choix != 'q'));

        menu(choix);
    }while(choix != 'q');

    return 0;
}


void menu(char choix){
    switch(choix){
        case 'c' :
            creer();
        break;
        case 'l' :
            lire();
        break;
        case 'e' : 
        break;
        case 's' : 
        break;
    }
}

void creer(){
    char path[64], name[64];
    char fullName[128];
    int fildes;
    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
    //~  O_RDONLY : Access Mode (Read Only)
    //~  O_CREAT : If the file does not exist it will be created
    //~  O_EXCL :  if this flag is specified in conjunction with O_CREAT, and pathname already exists, then open() will fail.
    int flags = O_RDONLY|O_CREAT|O_EXCL;
    printf("
%s-->Donner l'emplacement du fichier :%s ", CYAN_NORMAL, RESETCOLOR);
    scanf("%s", path);
    printf("%s-->Donner le nom du fichier :%s ", CYAN_NORMAL, RESETCOLOR);
    scanf("%s", name);
    snprintf(fullName, sizeof fullName, "%s/%s", path, name);
    fildes = ouvrir(fullName, flags, mode);
    if(fildes == -1){
        printf("
%sImpossible de créer le fichier. Réessayez plus tard. (%s)%s",RED_UNDERLINE,strerror(errno), RESETCOLOR);
    }else{
        printf("
%sLe fichier %s a été créé avec succès.%s", CYAN_BOLD, fullName, RESETCOLOR);
    }
    close(fildes);
}


int ouvrir(char *path, int flags, mode_t mode)
{
        return open(path, flags, mode);
}

header.h

#include <stdio.h>
#include <fcntl.h> // open function
#include <unistd.h> // close function
#include "colors.h"
#include "const.h"
#include <ctype.h>
#include <errno.h>
#include <string.h>
question from:https://stackoverflow.com/questions/19472546/implicit-declaration-of-function-close

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you included right headers? You need the following:

#include <fcntl.h> // for open
#include <unistd.h> // for close

do man open and man close on your terminal to find out what libraries they need for yourself


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...