I am new to C++ and i am stuck in the dilemma that how can i write code to read pipe separated string.
My program will have string as an input, the size of string will vary from 12 Bytes to Max. 255 bytes. The data in the string will be "|" (pipe) separated.
For Example : 112301900|501000200|......|720302130.
I want to read content of data stored between these pipe and stored it into local variable and then need to check this retrieved data position by position with given input time. If data matches with the given time which is also another input parameter then set one flag as true else set it as false.
#include <iostream>
#include <vector>
#include <string>
#include <regex>
int main()
{
const std::string str = "112301900|501000200|111111|2222222|3333333|4444|55555|720302130" ;
// use std::regex_token_iterator as a tokeniser
// http://en.cppreference.com/w/cpp/regex/regex_token_iterator
// place the tokens into a vector
const std::regex pipe( "\\|" ) ; // regex to match a single pipe character '|'
using iterator = std::sregex_token_iterator ;
const std::vector<std::string> tokens { iterator( str.begin(), str.end(), pipe, -1 ), iterator() } ;
// note: -1 iterates over non-matches ie, over sections between "|"
// print out the tokens which are in the vector
for( constauto& tok : tokens ) std::cout << tok << '\n' ;
// tip: to convert a token into a number, we can use std::stoll
// http://en.cppreference.com/w/cpp/string/basic_string/stol
}
I want to read content of data stored between these pipe and stored it into local variable and then need to check this retrieved data position by position with given input time. If data matches with the given time which is also another input parameter then set one flag as true else set it as false.