我试图从一个文件中读取,但我得到了这个错误。
/usr/include/c++/4.8/bits/basic_string.tcc:1068:5:注释:模板参数演绎/替换失败: prog.cc:35:38:注意:‘std::ofstream {aka std::basic_ofstream}’不是从‘std::basic_istream<_CharT,_Traits>’派生的
while(getline(input_file, line_)){
^我对C++比较陌生,所以我不知道这个错误到底意味着什么。任何帮助都将不胜感激,谢谢。这是我的密码。
#include "prog.hh"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
bool prog::readFile() {
ofstream input_file("myText.txt");
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_)){
cout<<line_<< '\n'; //prints the contents of the file into the console
}
input_file.close();
}
else {
cout<<"File is not open"<< '\n';
}
}发布于 2015-12-13 20:02:14
std::ofstream (out文件流)用于写入文件。您不能从std::ofstream中读取。使用std::ifstream (in文件流)读取文件,如果需要使用相同的流来读取和写入文件,则使用std::fstream。
发布于 2015-12-13 20:05:58
ofstream用于输出,您需要ifstream。
https://stackoverflow.com/questions/34255595
复制相似问题