2013-04-02 13 views
5

Ho davvero bisogno del tuo aiuto. Sembra che non riesca a manipolare i file in C++. Ho usato fstream fare qualche manipolazione di file, ma quando compilo esso, viene visualizzato un errore che dico:Errore fstream in C++

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

Qual è l'errore che ho fatto?

Ecco parte del codice sorgente:

#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; 
} 

risposta

6

Il fstream::open() che accetta std::string tipo come il nome del file è stato aggiunto in C++ 11. Compilare con il flag -std=c++11 o utilizzare fdir.c_str() come argomento (per passare invece const char*).

Nota che il costruttore fstream() può aprire il file, se fornito con il nome del file, che eliminerebbe la chiamata a 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 
    { 
    } 
} 
+0

Grazie mille hmjd! Questo ha funzionato. Immagino di non averlo compilato in C++ 11. –

4

È necessario abilitare C++ 11 modalità per std::basic_fstream<char>::open(std::string&, const openmode&) sovraccarico deve essere avaliable .

Passo uno di questi per gcc:

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

Prima di C++ 11, istream::open funzioni sono voluti solo C-stringhe. (Puoi chiamarlo pronunciando fp.open(fdir.c_str(), ios::app);)

+0

Grazie jrok, anche questo mi ha aiutato molto oltre alle risposte di hmjd. :) –