简体   繁体   中英

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 value of columns anyway be 2 or not?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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