Question:

Why wont this work ? readin in file names? c++?

by  |  earlier

0 LIKES UnLike

vector<string> read_all_files( vector<string> filenames )

{

vector<string> result;

string line;

for (vector<string>::iterator filename = filenames.begin();

filename != filenames.end();

filename++

) {

ifstream file( *filename ); ////////////////////////// THIS LINE IS PROBLEMATIC

if (!file) continue;

while (getline( file, line )) result.push_back( line );

file.close();

}

return result;

}

 Tags:

   Report

2 ANSWERS


  1. its been a while but i think you have to open the file when you use ifstream before you read it.

    ifstream file;

    file.open(*filename);


  2. The ifstream constructor takes a C-style string, i.e. char*, not std::string.  You can get a C-style string from std::string by the c_str() function.

    (*filename).c_str()

    Automatic conversion from std::string to char* was intentionally not included because automatic conversions can happen in unexpected places.  This way the conversion occurs only when the programmer explicitly asks for it.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.