2013-10-20 116 views
20

的隱式聲明我想結束與手柄相關的文件,但我發現從編譯器警告:功能「關閉」

的main.c:96:2:警告:隱式聲明的功能「關閉」 [-Wimplicit函數聲明]

這是我的代碼來源:

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

爲什麼我得到這個警告,我怎麼能解決呢?

這是整個代碼源:

的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

你從來沒有定義或聲明'open'和'close'什麼庫。你認爲他們神奇地來自哪裏? –

+10

@KerrekSB:在這裏扮演魔鬼的擁護者,提醒你,並不是每個人都像你一樣經驗豐富,知識淵博,你認爲魔法來自哪裏?我不需要定義或聲明它。 –

+0

@LightnessRacesinOrbit:假設你使用'for'是因爲你的C語言教科書告訴你......如果它告訴你使用'open'和'close',它肯定會提到如何在C語言中使用頭文件組織代碼! –

回答

44

已包含正確的頭?您需要:

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

man openman close你的終端上,找出他們需要爲自己

+0

我已經包含了這個頭文件,但是我仍然收到相同的警告:/,我也執行了man close命令,它顯示我是一個概要:#include ' – user2874861

+1

@ user2874861這真的很奇怪。如果您包含正確的標題,則不應該出現該錯誤。你可能會發布你的整個代碼?只需編輯你的問題並把你的代碼放在那裏 – sukhvir

+7

爲什麼在不同的頭文件中打開和關閉? O.o – Kevin