2013-10-20 16 views
20

Chcę zamknąć plik skojarzony z uchwytem, ​​ale ja dostaję ostrzeżenie kompilatora:niejawny deklarację funkcji „Zamknij”

main.c: 96: 2: ostrzeżenie: niejawna deklaracji z funkcją „Zamknij” [-Wimplicit-funkcja-deklaracji]

i to jest mój kod źródłowy:

#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); 

Dlaczego dostaję to ostrzeżenie i jak mogę go rozwiązać?

To jest cały kod źródłowy:

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;1H\e[2J"); 
     printf("\n\n%sMenu :%s\n", RED_BOLD, RESETCOLOR); 
     printf("\t(%sC%s)réer un fichier\n", RED_BOLD, RESETCOLOR); 
     printf("\t(%sL%s)ire un fichier\n", RED_BOLD, RESETCOLOR); 
     printf("\t(%sE%s)crire sur un fichier\n", RED_BOLD, RESETCOLOR); 
     printf("\t(%sS%s)upprimer un fichier\n",RED_BOLD, RESETCOLOR); 
     printf("\t(%sQ%s)uitter\n",RED_BOLD, RESETCOLOR); 
     do{ 
      printf("\n%sVotre choix :%s ",GREEN_BOLD,RESETCOLOR); 
      do { 
       c = getchar(); 
       choix = tolower(c); 
      } while (c == '\n'); 
     }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("\n%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("\n\t%sImpossible de créer le fichier. Réessayez plus tard. (%s)%s",RED_UNDERLINE,strerror(errno), RESETCOLOR); 
    }else{ 
     printf("\n\t%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> 
+2

Nigdy nie zdefiniowane lub uznane 'open' i' close'. Jak myślisz, skąd pochodzą magicznie? –

+10

@KerrekSB: Grając tutaj adwokatem diabła, aby przypomnieć, że nie wszyscy są tak doświadczeni i dobrze wiedzą jak ty, skąd, według ciebie, "magicznie" pochodzi? Nie musiałem tego definiować ani deklarować. –

+0

@LightnessRacesinOrbit: Prawdopodobnie używasz 'for', ponieważ twój podręcznik C mówi ci ... i jeśli mówi ci, żebyś używał' open' i 'close', to z pewnością wspomnie, jak kod jest zorganizowany z nagłówkami w C! –

Odpowiedz

44

Czy dołączono odpowiednie nagłówki? Potrzebne są następujące elementy:

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

zrobić man open i man close na terminalu, aby dowiedzieć się, jakie biblioteki muszą oni dla siebie

+0

Zawarłem ten nagłówek, ale wciąż otrzymuję to samo ostrzeżenie: /, Wykonałem również polecenie 'man close', które pokazuje mi jako SKŁADNIA:' #include ' – user2874861

+1

@ user2874861 To naprawdę dziwne. Nie powinieneś otrzymywać tego błędu, jeśli umieściłeś odpowiednie nagłówki. Możesz opublikować cały swój kod? Po prostu edytuj swoje pytanie i wpisz tam swój kod. – sukhvir

+7

Dlaczego 'open' i' close' są w różnych nagłówkach? O.o – Kevin