issue with std::fstream file handle reuse

Viewed 37

I am not sure what is wrong with this piece of code:

fstream file_h("h_input.txt");
        if(file_h.is_open()){
                while(!file_h.eof()){
                        line.clear();
                        getline(file_h, line);
                        v_hrml.push_back(line);
                }
                file_h.close();
                file_h.clear();
       }
       file_h("q_input.txt");
       if(file_h.is_open()){
                while(!file_h.eof()){
                        line.clear();
                        getline(file_h, line);
                        v_queries.push_back(line);
                }
                file_h.close();
                file_h.clear();
       }

For the code segment opening q_input.txt reusing file_h, the compiler generates the following error:

error: no match for call to ‘(std::fstream {aka std::basic_fstream<char>}) (const char [12])’
     file_h("q_input.txt");

Appreciate your thoughts.

TIA

1 Answers

You're trying to call a variable.

Change

file_h("q_input.txt");

to

file_h.open("q_input.txt");
Related