i read c++ ebook, then i make a code to open a file using fstream and use ios::in flag
here's the code :
#include <iostream>
#include <fstream>
using namespace std;
bool openFileIn(fstream&, string);
int main()
{
fstream dataFile;
if(openFileIn(dataFile, "sendy.txt"))
{
cout<<"succes";
}
else
cout<<"fail";
return 0;
}
bool openFileIn(fstream &file, string name)
{
bool status;
file.open(name, ios::in);
if (file.fail())
status = false;
else
status = true;
return status;
}
the code has problem with
file.open(name, ios::in);
then i read again, the problem can be solved by converting name(string object) to cstring :
file.open(name.c_str(), ios::in);
but there's no further explanation why i must convert the it to cstring, anyone knows?
and 1 more question, why do i should use reference variable to pass a fstream object?
edited :
i just tried the code using visual c++, and there's no problem with the string "name", is the problem because of the compiler?