从字符串数组变量打开文件
opening file from string array variables
我正在尝试编写一个程序,该程序将遍历多个文件。
string files[]={"file1.txt","file2.txt"};
int i=0;
ifstream fin;
while(i<2){
fin.open(files[i]);
fin.close();
i++;
}
这是我的代码的浓缩版本。我从fin.open行得到一个错误。我的编译器说:不能在当前范围内调用basic_ifstream<char,char_traits<char>>::open(files[i])
。
如果我放入实际的字符串(即fin.open("file1.txt")
),它会很好地工作,但我希望避免复制同一块代码8次。
有什么想法吗?
"我的同谋说:"
Can't call basic_ifstream>::open(files[i]) in current scope.
较旧的C++标准版本(以前的C++11)想要查看std::ifstream::open()
函数的const char*
参数,您可以只使用
fin.open(files[i].c_str());
以保持代码向后兼容。
相关文章:
- 通过多个头文件使用常量变量
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 无法通过头文件保存变量的值
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在 C++ 中将双精度变量写入二进制文件
- 如何使用文件中的文本,例如变量
- 定义C++新的环境变量并在 bat 文件中使用它
- 在头文件中定义变量不会出错
- 复制文件流C++静态变量
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- C++将文本从.txt文件解析为变量
- 从 C 中的变量获取文件的路径,C++
- 使用boost ::日志配置文件变量
- C++:帮助存储和显示输入文件变量
- 文件变量的问题
- 如何在先决条件列表中展开生成文件变量
- QT多个文件c++变量