据我所知,seekdir
必须设置当前流位置。但是如果我设置它 - 它d_name
在 next 之后的任何位置参数都返回相同的值readdir
。我只想读取手动输入位置的目录名称,不想在readdir
其中使用循环
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
using namespace std;
int main(int argc, char* argv[])
{
if(argc > 1)
{
DIR* directory = opendir(argv[1]);
if (directory == NULL)
{
cout << "Enter valid directory name" << endl;
return -1;
}
long position = 0;
cout << "Enter position of directory stream: ";
cin >> position;
seekdir(directory, position);
dirent *dir = readdir(directory);
cout << dir->d_name <<":"<< strlen(dir->d_name) << endl;
closedir(directory);
return 0;
}
else
{
return -1;
}
}