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

前端 未结 3 961
一个人的身影
一个人的身影 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:29

    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 See previous post here

提交回复
热议问题