我正在尝试模拟 ls 命令的行为。
我使用 dirent.h 库创建了一个简单的代码
DIR *dir;
dirent *pdir;
dir=opendir(".");
while((pdir=readdir(dir)))
{
cout<< pdir->d_name << endl;
}
closedir(dir);
我的当前目录包含两个文件 Screen.cpp 和 a.out。我希望此代码按顺序打印,例如ls -la
命令
.
..
a.out
Screen.cpp
但是这段代码输出它就像
.
Screen.cpp
..
a.out
有没有什么简单的方法可以在不存储/排序某个容器中的列表的情况下实现这一点?