我正在编写基本代码,但是尝试打开文件时遇到错误。我经历了一个艰难的休息,不得不从基础开始。以下是我遇到错误的代码部分:
int main()
{
string name;
fstream file;
cout << " Enter file name and type (E.g filname.txt) : ";
cin >> name;
file.open(name);
以下是错误:
91 16 D:\ Serryn \ test1.cpp [错误]没有匹配的函数来调用'std :: basic_fstream :: open(std :: string&)'
我经过很长的休息后返回,因此对任何不一致之处深表歉意。
如果std::basic_fstream::open(std::string&)
重载不可用,则可能使用的是C ++ 11之前的某些C ++版本。
请确保至少使用C ++ 11进行编译,应该没问题。
您也必须通过打开模式。
这里是一个例子:
std::basic_fstream::open(std::string&)
取自// print the content of a text file.
#include <iostream> // std::cout
#include <fstream> // std::ifstream
int main () {
std::ifstream ifs;
ifs.open ("test.txt", std::ifstream::in);
char c = ifs.get();
while (ifs.good()) {
std::cout << c;
c = ifs.get();
}
ifs.close();
return 0;
}
的代码
我建议您始终检查Here
非常有据可查!