ifstream open() with a variable

Hi,

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    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?

Thanks in advance, LuQ
Last edited on
closed account (oG8U7k9E)
...
Last edited on
Thanks, it worked!
Topic archived. No new replies allowed.