[英]fstream error in C++
我真的需要你的帮助。 我似乎无法在C ++中进行文件操作。 我使用fstream进行一些文件操作,但是当我编译它时,会出现一个错误:
|63|error: no matching function for call to 'std::basic_fstream<char>::open(std::string&, const openmode&)'|
我做错了什么?
以下是源代码的一部分:
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
inline int exports()
{
string fdir;
// Export Tiled Map
cout << "File to export (include the directory of the file): ";
cin >> fdir;
fstream fp; // File for the map
fp.open(fdir, ios::app);
if (!fp.is_open())
cerr << "File not found. Check the file a file manager if it exists.";
else
{
string creator, map_name, date;
cout << "Creator's name: ";
cin >> creator;
cout << "\nMap name: ";
cin >> map_name;
cout << "\nDate map Created: ";
cin >> date;
fp << "<tresmarck valid='true' creator='"+ creator +"' map='"+ map_name +"' date='"+ date +"'></tresmarck>" << endl;
fp.close();
cout << "\nCongratulations! You just made your map. Now send it over to tresmarck@gmail.com for proper signing. We will also ask you questions. Thank you.";
}
return 0;
}
在C ++ 11中添加了接受std::string
类型作为文件名的fstream::open()
。 使用-std=c++11
标志进行编译或使用fdir.c_str()
作为参数(以传递const char*
)。
请注意,如果提供文件名, fstream()
构造函数可以打开文件,这将消除对fp.open()
的调用:
if (std::cin >> fdir)
{
std::fstream fp(fdir, std::ios::app); // c++11
// std::fstream fp(fdir.c_str(), std::ios::app); // c++03 (and c++11).
if (!fp.is_open())
{
}
else
{
}
}
您需要为std::basic_fstream<char>::open(std::string&, const openmode&)
重载启用C ++ 11模式才能使用。
将其中一个传递给gcc:
-std=c++11
或-std=c++0x
在C ++ 11之前, istream::open
函数只接受C字符串。 (您可以通过说fp.open(fdir.c_str(), ios::app);
来调用它fp.open(fdir.c_str(), ios::app);
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.