I'm trying to use .open() but I'm having an issue with my arguments to this function. Here's my code:

vector<string> FDT_filename;
    ifstream is;
    is.open (FDT_filename[0], ios::binary );

Which results in the following error:

../sim_main.cpp:297: error: no matching function for call to ‘std::basic_ofstream<char, std::char_traits<char> >::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::_Ios_Openmode&)’
/usr/include/c++/4.2.1/fstream:650: note: candidates are: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]

In what way is my FDT_filename not correct? Does it need to be converted to a character and if so how? Keep in mind that FDT_filename[..] can be of arbitrary length.

Edited 6 Years Ago by dansnyderECE: n/a

use the .c_str() conversion for string. It's expecting a const char* argument.

vector<string> filenames;
filenames.push_back("test.txt");
ifstream inputFile;
inputFile.open(filenames[0].c_str(), ios::binary);

First, FDT_filename[0] is not valid because there is nothing inside the vector.
But to fix your error, use FDT_filename[0].c_str().

Okay, works well now. Thanks. I didn't post my entire code, is the absence of a definition of the vector element the reason why you say this is not valid?

> is the absence of a definition of the vector element the reason why you say this is not valid?
Probably. If the vector is empty then v[0] is an out of bounds access.

This article has been dead for over six months. Start a new discussion instead.