know the number of columns from text file, separated by space or tab

前端 未结 3 965
一个人的身影
一个人的身影 2021-01-22 07:01

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         


        
3条回答
  •  心在旅途
    2021-01-22 07:37

    You can do this very easily if these three assumptions are true:

    1. dummyLine is defined so that you have access to it outside the while loop scope
    2. The last line of the file has the same tab/space delimited format (Cause that's what dummyLine will contain after the while loop)
    3. One and only one tab/space occurs between numbers on each line

    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;
    

提交回复
热议问题