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.

Dani AI

Generated

Short answer: the compiler/library you're using expects a C-style filename pointer, so converting your std::string to const char* fixes the immediate error — that’s what pointed out. Modern C++ (since C++11) added overloads that accept std::string directly, and C++17 added std::filesystem::path overloads for even better portability. (en.cppreference.com)

Beyond the fix, two practical things to watch for in this thread: first, indexing an empty vector with v[0] is undefined behavior — check v.empty() or v.size() first, or use v.at(0) if you want bounds-checked access that throws std::out_of_range. and were right to flag that. ()

Second, always verify the stream after opening. Calling is_open() or testing the stream as a boolean immediately after open() lets you detect failures (wrong path, permissions, CWD issues) instead of continuing on with a bad stream. That check is quick and saves debugging time. (cppreference.patrickfasano.com)

If you plan to modernize the codebase: compile with C++11 or later and you can pass std::string to ifstream/ofstream constructors or open() directly; with C++17 prefer std::filesystem::path for portable path handling (helps on Windows with native wide paths). If you must support pre-C++11 toolchains, keep using .c_str() and the sanity checks above. (en.cppreference.com)

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.