[英]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.