开发者

Counting columns of a space separated file

My question concerns the use of std::count (or another appropriate function) to count the columns of a space separated file.

I currently use something like this:

  std::ifstream inFile("file"); 
  int lines = std::count(std::istreambuf_iterator<char>(inFile), 
             std::istreambuf_iterator<char>(), '\n');

to count the lines.

Since all the lines are equal (same amount of data), would something like

  std::ifstream inFile("file"); 
  int columns = std::count(std::istreambuf_iterator<char>(inFile), 
             std::istreambuf_iterator<char>('\n'), ' ') + 1;

do what I need?

Thanks

EDIT:

I mean, if in "file" there is data like 1 2 or 1 [many spaces here] 2, would the valu开发者_Go百科e of columns anyway be 2 or not?


No, you'll count spaces, not columns. You need to tokenize your line, e.g. by boost::tokenizer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜