Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ type of argument to ifstream::open()

Tags:

c++

ifstream

What type must I make my file name to use it as an argument to ifstream.open()?

int main(int argc, char *argv[]) {
    string x,y,file;

    string file = argv[1];
    ifstream in;
    in.open(file);
    in >> x;
    in >> y;
    ...

With this code, I get the following error:

main.cpp|20|error: no matching function for call to 'std::basic_ifstream<char,
     std::char_traits<char> >::open(std::string&)'|
gcc\mingw32\4.4.1\include\c++\fstream|525|note: candidates are: void std::basic_ifstream<_CharT,
     _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]|

UPDATE:

i get this error enter image description here

like image 848
Jaanus Avatar asked Feb 25 '23 20:02

Jaanus


1 Answers

The constructor takes a const char* (http://www.cplusplus.com/reference/iostream/ifstream/ifstream/) so you should do it like this:

in.open(argv[1]);

or if you really want to use the file string variable, then

in.open(file.c_str());
like image 191
Marius Bancila Avatar answered Feb 27 '23 10:02

Marius Bancila