如何使用目录d_name打开目录
how to open a directory using its d_name
我列出了目录,然后想打开其中一个条目的d_name。
为此,我输入一个整数,即列表中项目的数量。并打开其相应的文件。
我收到此错误:
[Error] request for member 'c_str' in 'ent->dirent::d_name', which is of non-class type 'char [260]'
法典:
#include <bits/stdc++.h>
#include <fstream>
#include <direct.h>
#include <Windows.h>
#include <conio.h>
#include <dirent.h>
using namespace std;
int main(){
DIR *dir;
struct dirent *ent;
if ((dir = opendir ("C:/path")) != NULL) {
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL) {
cout<<ent->d_name<<endl;
}
}
else {
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}
int z;
cout<<"enter the directory number to open"<<endl;
cin>>z;
int k{1}; // counter variable
dir = opendir ("C:/path")
while ((ent = readdir (dir)) != NULL) {
if(z==k){
system((ent->d_name).c_str());
break;
}
k++;
}
}
删除对c_str()
的调用。d_name
是字符数组(C 样式字符串(,而不是std::string
类型。
相关文章:
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 在 C++ 中使用命名空间时出现"does not name a type"错误
- 在模板化类中使用嵌套嵌套类时的'dependent name is not a type'
- 无法使用其他文件中的枚举:" does not name a type"错误
- 错误:使用 char C++ "uninitialsed local variable 'name' used" *
- C++类创建"does not name a type"(使用 Arduino 和颜色传感器)
- 尝试使用指向函数的指针时出错"does not name a type"
- 使用 LLVM 的 libc++ 时,__1 符号从何而来?
- 在 64 位 Windows 7 上使用 MinGW 进行编译时"Error: bad register name `%rax'"
- 在std::cout之后使用std::cin时,换行符从何而来
- 为什么我使用这些意大利面模板会"error: type name is not allowed"?
- 为什么以及在何处在C++中使用引用和指针
- 是否存在使用name=type;的私有项;
- 编译器错误:使用 g++ 编译时"does not name a type"宏
- 使用 foo::bar 不会屏蔽 typedef-name ::bar?
- 在类模板定义中使用template-name代替template-id
- 使用typeinfo::name()后内存泄漏
- 无效地使用没有参数列表的template-name Queue
- 使用未初始化的局部变量"name"
- 错误:"dependent name is not a type" 。当在类中使用 typedef 类型作为返回值时,带有模板