I'm currently trying to open a file which name (file.txt) is stored in a variable. But it doesn't work. I just can't understand what's wrong with my code:
string sz_line, sz_config_content;
string sz_config_path = "config.cfg";
// Load config file
ifstream file_config;
file_config.open(sz_config_path);
if (file_config.is_open())
{
// Insert every line of [file] into the string sz_config_content
while (!file_config.eof() )
{
getline (file_config, sz_line);
sz_config_content += sz_line + " ";
}
// Close [file]
file_config.close();
}
else // File could not be opened
{
cout << "Unable to open file";
}
This gives a compiler error. But when I change:
file_config.open(sz_config_path); to: file_config.open("config.cfg"); it suddenly works..
Why can't I load a filename thats stored in a string?