mercredi 22 janvier 2020

Reading in a list of csv floats with a prefix in C++

I am trying to read in a list of floats formatted like the example below from a file and into a vector, and print them back out again. Is there a way to modify the stream or the iterator to drop the 'f' that indicates float and correctly parse the data with minimal changes?

1.5f, 2.0f, 4.0f, 1.0f, 1.0f, 2.0f, 4.0f, 2.0f, 1.0f, 0.0f, 0.0f, 1.0f, 9.0f

Code here

std::ifstream infile("matrices.txt");
std::string s;
std::vector<float> A;

std::getline(infile,s,'\n');
std::stringstream mss(s);
std::copy(std::istream_iterator<float>( mss ), std::istream_iterator<float>(),std::back_inserter(A));    
std::copy(A.begin(), A.end(), std::ostream_iterator<float>(std::cout, ", "));

Aucun commentaire:

Enregistrer un commentaire