C语言 函数 'close' 的隐式声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19472546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implicit declaration of function ‘close'
提问by user2874861
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]
main.c:96:2: 警告:函数“close”的隐式声明 [-Wimplicit-function-declaration]
And this is my code source:
这是我的代码源:
#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 I'm getting this warning, and how can I solve it?
为什么我会收到此警告,我该如何解决?
This is the whole code source :
这是整个代码源:
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
头文件.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>
回答by sukhvir
Have you included right headers? You need the following:
您是否包含了正确的标题?您需要以下内容:
#include <fcntl.h> // for open
#include <unistd.h> // for close
do man openand man closeon your terminal to find out what libraries they need for yourself
做man open并man close在您的终端上找出他们自己需要的库

