linux 文件管理相关函数介绍

来源:互联网 发布:如何在淘宝社区发帖 编辑:程序博客网 时间:2024/06/07 05:54

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"}

原创粉丝点击