material/c++/linux/dir.cpp

24 lines
395 B
C++
Raw Normal View History

2024-10-27 09:23:50 +00:00
#include<iostream>
#include<unistd.h>
#include<dirent.h>
using namespace std;
int main()
{
char buf[255];
if(getcwd(buf, 255) == NULL)
cout << "error" << endl;
else cout << buf << endl;
DIR *ddir;
if((ddir = opendir(buf)) == NULL)
cout << "error" << endl;
struct dirent* entry;
while((entry = readdir(ddir)) != NULL)
cout << entry->d_name << endl;
closedir(ddir);
return 0;
}