您的位置:首页 > 运维架构 > Linux

linux 文件管理相关函数介绍

2012-11-24 18:25 393 查看
fileno函数:文件流(FILE*)->文件描述符

#include <stdio.h>

int main(void)

{

 FILE * fp;  //文件流

int fd;   //文件描述符

fp=fopen("/home/yaoyin/bin/test/1.c","r");

fd=fileno(fp);  

printf("fd=%d\n",fd);

fclose(fp);

return 0;

}

结果: fd=3(0 stdin 1 stdout 2 stderr 未被使用的最小的描述符=3)

 

扫描目录:

头文件dirent.h

DIR:结构体,用于操作目录,不可改变之

dirent:结构体,与DIR相同,可操作之

Directory entries themselves are returned in dirent structures,also declared in dirent.h,

because one should never alter the fields in the DIR structure directly.

相关函数:opendir,closedir,readdir,telldir,seekdir

DIR *opendir(const char *name);

关键函数:readdir

struct dirent *readdir( DIR * dirp)

return a pointer to a structure detailing the next directory entry in the directory stream drip.

successive calls return further directory entries.

On error,or at the end of the directory,return NULL.

当其他进程创建、删除文件的同时,使用readdir,不能保证列举出所有的文件和子目录。

//1.c

int main(int argc,char *argv[])

{

  //

}

gcc 1.c -o test

./test arg1 arg2

结果:argc=3  argv指向{"test","arg1","arg2"}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐