2013-04-02 7 views
5

Ich brauche wirklich Ihre Hilfe. Es scheint, dass ich Dateimanipulation in C++ nicht machen kann. Ich benutzte fstream einige Dateimanipulation zu tun, aber wenn ich es kompilieren, erscheint ein Fehler, die sagen:fstream Fehler in C++

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

Was ist der Fehler, den ich gemacht habe?

Hier ist ein Teil des Quellcodes:

#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 [email protected] for proper signing. We will also ask you questions. Thank you."; 
} 
return 0; 
} 

Antwort

6

Die fstream::open() die std::string Typ wie der Dateiname C++ 11 wurde hinzugefügt akzeptiert. Kompilieren Sie entweder mit -std=c++11 Flag oder verwenden Sie fdir.c_str() als das Argument (statt const char* statt).

Beachten Sie, dass der fstream() Konstruktor kann die Datei öffnen, wenn mit dem Dateinamen versehen, die den Anruf zu fp.open() beseitigen würde:

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 
    { 
    } 
} 
+0

Vielen Dank hmjd! Das hat funktioniert. Ich schätze, ich habe es einfach nicht nach C++ 11 kompiliert. –

4

Sie benötigen C++ 11-Modus für std::basic_fstream<char>::open(std::string&, const openmode&) Überlastung sein avaliable ermöglichen .

Pass eine dieser gcc:

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

Vor C 11 ++, nahm istream::open Funktionen nur C-Strings. (Sie können es anrufen, indem Sie sagen fp.open(fdir.c_str(), ios::app);)

+0

Danke jrok, das hat mir auch sehr geholfen, abgesehen von hmjds Antworten. :) –

Verwandte Themen