我在C++里完全是新手,这就是我的问题。我正在尝试编写一个C++程序,它从用户那里读取路径并将目录的内容打印到屏幕上。我能读一个目录并打印出来,但我不能让它从外部读出来。
#include <iostream>
#include <dirent.h>
using namespace std;
int main()
{
struct dirent *entry;
int files = 0;
DIR *folder;
const char *path="c:\\";
folder = opendir(path);
if (folder == NULL)
{
cout << "Unable to open the directory \n\n";
return(1);
}
else
{
cout << "Directory opened\n\n";
}
while ((entry=readdir(folder)))
{
files++;
cout << "File " << files << " " << entry->d_name << endl;
}
closedir(folder);
return(0);
}
我只想创建一个从用户读取路径并将值传递给main函数的函数,但不知何故,“path”是常量char这一事实不允许我这样做。
很抱歉我的无知。。。我在C++的经验只有2天。。。。
由于这是C++,您可以执行以下操作来读取路径:
cin >> path;