C++对参数1没有已知的转换-从文件中读取
C++ no known conversion for argument 1 - reading from a file
我一直在开发一个文件读取器,它从文件内部读取数据并将其存储在字符串中。当我硬编码要打开的文件名时,我的代码会编译它应该如何编译,但当我试图从键盘读取文件时,我会得到错误"参数1从str到const没有已知的转换,我不知道为什么
#include <iostream>
#include <fstream>
#include <string>
int main() {
string fileName;
cout << "Enter the file name to be read: ";
cin >> fileName;
ifstream input_file(fileName);
std::string line_; // string which text file contents will be stored in
if(input_file.is_open()){ // validation to see if the file is open
while(getline(input_file, line_)){
std::cout<<line_<< 'n'; //prints the contents of the file into the console
}
input_file.close();
}
else {
std::cout<<"File is not open"<< 'n';
}
std::cin.get();
}
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::string fileName;
std::cout << "Enter the file name to be read: ";
std::cin >> fileName;
std::ifstream input_file(fileName.c_str());
std::string line_; // string which text file contents will be stored in
if(input_file.is_open()){ // validation to see if the file is open
while(getline(input_file, line_)){
std::cout<<line_<< 'n'; //prints the contents of the file into the console
}
input_file.close();
}
else {
std::cout<<"File is not open"<< 'n';
}
std::cin.get();
}
祝你好运。。。
可能是旧编译器不能将字符串作为参数。使用cstring:
ifstream input_file(fileName.c_str());
或者在命令行中指定-std=c++11
以获得C++11支持。
相关文章:
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将生成文件转换为 CMakeLists.txt等效项
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 如何将 UTF-8 文本从文件转换为某个可以迭代的容器,并检查每个符号是否为C++字母数字?
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 将文件复制到自定义位置,存在字符串转换问题
- 使用从文件(stod、strtod、atof)中提取的数据C++从字符串转换为双精度.csv
- 将模板类转换为标头和 cpp 文件
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- 如何使用 C++将 2D RGB 数组转换为 dds 文件?
- 如何将二进制文件头示例代码 (C++/C#) 转换为 Python?
- 在 c++ 中逐行读取文件(将代码从 Python 转换为 C++)
- 将调用类转换为库本身后,库头文件上没有这样的文件
- 如何使用C++将.trc文件类型转换为文本文件?
- 将 boost::文件系统::p ath 转换为 char?
- 如何将 .ui 完全转换为 C++ 头文件和源文件
- 转换 mp4 文件并将其流式传输到带有树莓派的查看器
- Makefiles:转换文件扩展名
- .cpp错误:与"std::cerr "中的"运算符<"不匹配<"正在转换文件 \" ""
- 从c++基础转换文件