Fstream error in C ++

I really need your help. It seems that I cannot do file manipulations in C ++. I used fstream to do some file manipulation, but when I compile it, an error message appears:

|63|error: no matching function for call to 'std::basic_fstream<char>::open(std::string&, const openmode&)'|

What mistake did I make?

Here is part of the source code:

#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 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;
}
+5
source share
2 answers

fstream::open()which accepts a type std::stringsince the file name was added in C ++ 11. Either compile the flag -std=c++11or use it fdir.c_str()as an argument (instead const char*).

Note that the constructor fstream()can open the file if it is provided with a file name, which eliminates the call 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
    {
    }
}
+6
source

++ 11 std::basic_fstream<char>::open(std::string&, const openmode&), .

gcc:

-std=c++11 -std=c++0x

++ 11 istream::open C-. ( , fp.open(fdir.c_str(), ios::app);)

+4

All Articles