12345678910111213141516171819
string getColorFileName(string familyName, string colorIndexFileName) { fstream colorfile; string colorfamily = ""; // This is the variable colorfile.open(baseDir + colorIndexFile); string color, fn; string fileName; cout << colorfamily << "Test" << endl; while (!colorfile.eof()) { colorfile >> color; colorfile >> fn; if (color == colorfamily) { fileName = fn; break; } } colorfile.close(); // return fileName; }
1234567891011121314151617181920212223
#include <iostream> #include <string> #include <fstream> std::string getColorFileName( std::string familyName, std::string colorIndexFileName ) { std::ifstream file(colorIndexFileName) ; // open the file std::string colour, name_of_file ; // note: this is canonical; do not loop on eof() while( file >> colour >> name_of_file ) if( colour == familyName ) break ; return name_of_file ; // return an empty string if not found } int main() { std::string colour_family = "something" ; std::string file_name = "some path" ; // pass colour_family (and file_name) to the function std::string colour_file_name = getColorFileName( colour_family, file_name ) ; }