【发布时间】:2021-01-13 11:00:38
【问题描述】:
我需要将此代码的结果输出到 txt。当我尝试时它只保存一行。
using namespace std;
std::string c;
void list_dir(const char *path) {
struct dirent *entry;
DIR *dir = opendir(path);
if (dir == NULL) {
return;
}
while ((entry = readdir(dir)) != NULL) {
c = entry->d_name;
cout << c << endl;
}
closedir(dir);
}
谢谢
【问题讨论】:
-
输出是什么,你期望什么输出?
-
我看到您正在使用 POSIX 的文件系统 API。您是否考虑过改用 C++ 标准文件系统库?
-
"当我尝试时它只保存一行。" - 你的程序不会“保存”任何东西 - 它只调用
puts打印到stdout。我想这就是你的意思? -
请将您的问题集中在实现多行或将输出放入文件中。
-
请将显示的代码片段升级为minimal reproducible example。