linux 文件管理相关函数介绍

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值