In the main function, there are various vectors of different template types(float, int, char*). This function is called to read in formatted input from different files to fill each vector. My problem is coming from the type conversion since
v.push_back((T)(pChar));
does not like converting char* to float(presumably because of the decimal point).
Question: Is there a way to get correct conversions regardless of the data type as long as the input file is appropriate? (I've considered typeid(); but am not sold on using it)
template <class T>
void get_list(vector <T> & v, const char * path)
{
fstream file;
const char delim[1]{' '};
char line[512];
char * pChar;
file.open(path, ios_base::in);
if (file.is_open())
{
while (!file.eof())
{
file.getline(line, 512);
pChar = strtok(line, delim);
while (pChar != NULL)
{
v.push_back(pChar);
pChar = strtok(NULL, delim);
}
}
file.close();
}
else
{
cout << "An error has occurred while opening the specified file." << endl;
}
}
This is homework but this problem does not pertain directly to the objective of the assignment. The assignment is on heaps for data structs/algorithm class.