c++中的fstream错误
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);
)
相关文章:
- 为什么 fstream 在打开带有格式的文件时会导致分段错误?
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?
- C++ 错误检查 fstream open() 命令和一般字符串流错误处理
- 变量 std::fstream 文件具有初始值设定项,但类型不完整 编译错误
- std::fstream 已使用,但编译器抛出错误
- 为什么错误"permission denied","id returned 1 exit status"仅在 IM 使用 C++ 中的头文件 fstream 时才出现
- fstream.h 中的 STL 命名空间错误与 qmake/make.
- 为什么我的简单C++程序使用队列和 fstream 错误到无效指针
- C Fstream字符串组合错误
- Fstream中的写功能将在Windows 10上引起错误
- fstream错误无法在C 中创建或打开文件
- CMD窗口停止工作,文本文件使用fstream读取错误
- 在结构中使用fstream,错误
- Ubuntu 13.10 中的 GCC iostream fstream 错误
- 如何在将 fstream 数据写入磁盘时检测致命错误
- 编译器C++中的 fstream 错误
- 在c++中使用同一个fstream对象进行读写时,是否有任何导致错误的特性
- fstream:检查错误的最正确方法
- 为什么我得到一个错误说eof不是fstream的类类型
- fstream 错误,"streambuf"当我尝试编译和运行时