I need to know the number of columns from a text file with floats.
I\'ve made like this to know the number of lines:
inFile.open(pathV);
// checks if f
Given a line you have read, called line this works:
std::string line("189.53 58.867 74.254 72.931 80.354");
std::istringstream iss(line);
int columns = 0;
do
{
std::string sub;
iss >> sub;
if (sub.length())
++columns;
}
while(iss);
I don't like that this reads the whole line, and then reparses it, but it works.
There are various other ways of splitting strings e.g. boost's <boost/algorithm/string.hpp> See previous post here
You can read one line, then split it and count number of elements.
Or you can read one line, then iterate through it as an array and count number of space and \t characters.
You can do this very easily if these three assumptions are true:
dummyLine is defined so that you have access to it outside the while loop scopedummyLine will contain after the while loop)If all these are true, then right after the while loop you'll just need to do this:
const int numCollums = std::count( dummyLine.begin(), dummyLine.end(), '\t' ) + std::count( dummyLine.begin(), dummyLine.end(), ' ' ) + 1;