Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fstream error in C++

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

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

What is the mistake I've done?

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'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;
}
like image 845
Sean Francis N. Ballais Avatar asked Apr 02 '13 10:04

Sean Francis N. Ballais


People also ask

Can I use ifstream in C?

Because an ifstream IS an istream, anything you can do to an istream you can also do the same way to an ifstream. In particular, cin is an example of an istream, so anything that you can do with cin you can also do with any ifstream.

Is fstream C or C++?

No, fstream is a C++ class. C does not have classes, and consequently does not have an fstream class in its standard.

What does fstream stand for?

The fstream term stands for File Stream. Stream refers to a sequence of characters moving from the disk to the C++ program or from the C+ program to the disk. Moving characters from a file in disk to the program is inputting. Moving characters from the program to a file in the disk is outputting.

What is fstream used for in C++?

fstream: This Stream class can be used for both read and write from/to files.


2 Answers

The fstream::open() that accepts std::string type as the file name was added in C++11. Either compile with -std=c++11 flag or use fdir.c_str() as the argument (to pass const char* instead).

Note that the fstream() constructor can open the file if provided with the file name, which would eliminate the call to 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
    {
    }
}
like image 152
hmjd Avatar answered Oct 23 '22 14:10

hmjd


You need to enable C++11 mode for std::basic_fstream<char>::open(std::string&, const openmode&) overload to be avaliable.

Pass one of these to gcc:

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

Prior to C++11, istream::open functions took only C-strings. (You can call it by saying fp.open(fdir.c_str(), ios::app);)

like image 41
jrok Avatar answered Oct 23 '22 14:10

jrok