Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 30th, 2012  |  syntax: None  |  size: 0.67 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Get next float from an istream?
  2. Data.txt:
  3. blah blah blah blah blah
  4. blah blah blah blah blah
  5. blah 0.94 blah blah blah
  6.  
  7. std::istream inputFile(Data.txt);
  8. float myNumber = inputFile.GetNextFloat();
  9.  
  10. std::cout << myNumber << std::endl; // Prints "0.94"
  11.        
  12. #include <iostream>
  13. #include <string>
  14. #include "boost/regex.hpp"
  15. namespace re = boost;
  16.  
  17. int main()
  18. {
  19.     re::regex floatre("^[^-+0-9]*([-+]?[0-9]+\.[0-9]+)(.*)");
  20.     for (std::string line; std::getline(std::cin, line); )
  21.     {
  22.         re::smatch results;
  23.         while (re::regex_match(line, results, floatre))
  24.         {
  25.             std::cout << "  float='" << results[1] << "'n";
  26.             line = results[2];
  27.         }
  28.     }
  29. }