简体   繁体   English

计算空格分隔文件的列

[英]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. 我的问题涉及使用std::count (或另一个适当的函数)来计算空格分隔文件的列。

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 value of columns anyway be 2 or not? 我的意思是,如果在"file"1 21 [many spaces here] 2 ,那么columns的值还是为2?

No, you'll count spaces, not columns. 不,您将计算空格,而不是列。 You need to tokenize your line, eg by boost::tokenizer 您需要标记您的行,例如通过boost :: tokenizer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM