If you need to read a line from a file, you can use std::ifstream:
std::ifstream fs( "some_file.txt" );
std::string input_string;
while ( getline(fs, input_string) ) {
}
Then, just for fun, the regex version, as you could parse the input string:
#include <iostream>
#include <boost/regex.hpp>
#include <boost/lexical_cast.hpp>
int main()
{
const std::string input_string = "abc=14,22 xyz=33, tdc=48";
boost::regex expr( "^abc=(\\d+,\\d+) xyz=(\\d+), tdc=(\\d+)$" );
boost::match_results<std::string::const_iterator> what;
if ( regex_search( input_string, what, expr )) {
std::string abc_str( what[1].first, what[1].second );
std::replace( abc_str.begin(), abc_str.end(), ',', '.' );
float abc = boost::lexical_cast<float>( abc_str );
std::string xyz_str( what[2].first, what[2].second );
int xyz = boost::lexical_cast<int>( xyz_str );
std::string tdc_str( what[3].first, what[3].second );
int tdc = boost::lexical_cast<int>( tdc_str );
std::cout << abc << std::endl;
std::cout << xyz << std::endl;
std::cout << tdc << std::endl;
}
return 0;
}
In your particular question, using Regex is redundant, but in more complex cases it can be useful.
source
share