/*
**系统调用:打开一个文件
**函数原型:int open(const char *filename,int flags,mode_t mode);
**参数:filename->要打开的文件名(包含路径)
flags->以什么样的方式打开
mode->创建模式
返回值:???
*/
#include <stdio.h>
#include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(int argc,char *argv[])
{ int fd; if(argc<2) { puts("please input the open file pathname!\n"); exit(1); } if((fd=open(argv[1],O_CREAT | O_RDWR,0755))<0)//参数argv[1]按理来说是文件名了,那argv[2] 、argv[3].......呢????
{ perror("open file failure!\n"); exit(1); } else { printf("open file %d success!\n",fd); //fd返回值的类型,到底是什么样的一个值呢?? } close(fd); exit(0); } 实验如下: [root@localhost File]# ls
file_creat.c file_open.c [root@localhost File]# vi file_open.c [root@localhost File]# gcc file_open.c -o file_open [root@localhost File]# ls file_creat.c file_open file_open.c [root@localhost File]# ./file_open hello open file 3 success! //这里说明了返回值fd=3,可真的不知道什么来计算这个返回值的值?? [root@localhost File]# ls file_creat.c file_open file_open.c hello 解答:上述涉及到linux“文件描述符的知识”,在linux内核中,文件描述符分为标准输入stdin、标准输出stdout、标准错误输出stderror,分别是0、1、2,这些数存在一个数组中 ,fd=open()调用后,也返回一个文件描述符,fd的值从3、4、5.......开始,然后存在0、1、2数组之后。
|
Linux应用程序-文件编程-file_open()函数疑问
最新推荐文章于 2025-03-20 23:20:03 发布