
Untitled
By: a guest on
Apr 30th, 2012 | syntax:
None | size: 0.67 KB | hits: 14 | expires: Never
Get next float from an istream?
Data.txt:
blah blah blah blah blah
blah blah blah blah blah
blah 0.94 blah blah blah
std::istream inputFile(Data.txt);
float myNumber = inputFile.GetNextFloat();
std::cout << myNumber << std::endl; // Prints "0.94"
#include <iostream>
#include <string>
#include "boost/regex.hpp"
namespace re = boost;
int main()
{
re::regex floatre("^[^-+0-9]*([-+]?[0-9]+\.[0-9]+)(.*)");
for (std::string line; std::getline(std::cin, line); )
{
re::smatch results;
while (re::regex_match(line, results, floatre))
{
std::cout << " float='" << results[1] << "'n";
line = results[2];
}
}
}