我是C++的新手,我正在尝试使用dirent.h头来操作目录项。下面的小应用程序可以编译,但在您对目录名称进行了柔和处理后,它会出现puke。有人能给我点提示吗?int quit是用来提供while循环的。我删除了这个循环,试图隔离我的问题。
谢谢!
#include <iostream>
#include <dirent.h>
using namespace std;
int main()
{
char *dirname = 0;
DIR *pd = 0;
struct dirent *pdirent = 0;
int quit = 1;
cout<< "Enter a directory path to open (leave blank to quit):\n";
cin >> dirname;
if(dirname == NULL)
{
quit = 0;
}
pd = opendir(dirname);
if(pd == NULL)
{
cout << "ERROR: Please provide a valid directory path.\n";
}
return 0;
}发布于 2010-06-13 02:27:30
如果您使用的是C++,请不要使用char *或数组,请使用std::string:
#include <string>
....
string dirname;
cout<< "Enter a directory path to open (leave blank to quit):\n";
getline( cin, dirname );
if ( dirname == "" ) {
exit(1);
}
....
pd = opendir(dirname.c_str() );发布于 2010-06-13 02:23:27
更改:
char *dirname = 0;至:
char dirname[PATH_MAX] = "";https://stackoverflow.com/questions/3029633
复制相似问题