目录
一:遍历获取子文件或子文件夹的名称
可以查看下opendir函数说明手册
代码实现
测试1:可以先看看家目录下的子文件或子文件夹名
#include<iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
using namespace std;
int main()
{
DIR* dir = NULL;
struct dirent* pdir = NULL;
if ((dir = opendir("/root")) == NULL)
{
perror("opendir error");
}
else
{
while ((pdir = readdir(dir)) != NULL)
{
cout << "子文件或子文件夹的名称 = " << pdir->d_name << endl;
}
}
return 0;
}
控制台查看结果
带.的是隐藏目录文件
cd /:根目录(计算机目录)
cd./: 代表当前文件夹
cd../:代表此文件夹上一级
即便.和..算是子文件或子文件夹
但实际应用中也最好要将.和..给屏蔽掉
测试2:遍历root下的文件名为3的文件 中子文件或子文件夹名
if ((dir = opendir("/root/3")) == NULL)
不难看出,.和..是子文件或子文件夹名
如果可以屏蔽最好就屏蔽掉
二:chmod权限设置
chmod 777 文件: 修改单个文件权限
chmod -R 777 文件 :修改子文件夹中所有文件权限
三:mkdir&rmdir
mkdir:创建文件夹
rmdir:删除文件夹;注意点:删除一个空目录,文件夹中不能包含任何子文件,否则不能删除
四:目录访问
opendir返回
DIR* 结构体指针
包含很多属性
如下一个文件 就具备有多个不同属性