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.

Recommended Answers

All 5 Replies

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.

Okay good, still touch and go here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.