Hi, I have been trying for several hours without success to write a function that takes a filename, opens the corresponding file, populates a std::vector<double> with values from the file, and returns the std::vector<double>. I can't get past the passing the filename stage unfortunately, despite reading about this here there and everywhere online.

Could anybody give me a clue how to do this please?

Thanks!

Recommended Answers

You can do this by passing a pointer to char or a string.

#include <string>
void yourFunction(const char * filename); //we don't want to modify so it's const
void yourFunction(string filename);
Jump to Post

All 2 Replies

You can do this by passing a pointer to char or a string.

#include <string>
void yourFunction(const char * filename); //we don't want to modify so it's const
void yourFunction(string filename);

Also, if you're using std::strings for a filename you'll have to convert them to const char* for ifstream. The c_str() function will do that for you:

std::string filename = "my_file.txt";
    std::ifstream infile(filename.c_str());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.