据我所知,seekdir
必须设置当前流的位置。但是,如果我设置了它,它会在下一个readdir
之后为任何位置参数返回相同的d_name
。我只是想为手动输入位置读取目录名,并且不希望在它无法设置流的位置与seekdir()
#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;
}
}
从手册页:*“loc参数应该是之前调用telldir(3)返回的值。”*看起来您并不像从telldir()获得参数。 – cdhowie 2013-05-01 20:47:21
'telldir()'返回0.那么如何设置流的位置? – vmeln 2013-05-01 20:49:36
通常你使用'seekdir()'返回到你之前的一个点,使用'telldir()'记录的值。 – cdhowie 2013-05-01 20:51:49