C ++没有已知的参数1转换 - 从文件读取

时间:2015-12-13 23:12:22

标签: c++

我一直在研究文件阅读器,它从文件内部读取数据并将其存储在字符串中。我的代码编译时我应该如何硬编码要打开的文件名,但是当我尝试从键盘读取文件时,我得到错误"没有已知的转换参数1从str到const和I不知道为什么。继承我的代码:

#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();
}

2 个答案:

答案 0 :(得分:1)

#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();
}
祝你好运......

答案 1 :(得分:0)

可能不会将字符串作为参数的旧编译器。使用cstring:

 <form data-bind="submit: addMessage">
    <input type="text" data-bind='textInput: currentMessage().content' />
    <button id="ButtonSendMessage" type="submit">Send</button>
</form>

或者在命令行上指定ifstream input_file(fileName.c_str()); 以获得C ++ 11支持。