Reading content of pipe seperated string

Hi,

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.

Any suggestion for writing this program?
Using the standard regular expressions library may yield the simplest solution:
http://en.cppreference.com/w/cpp/regex

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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( const auto& 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 
}

http://coliru.stacked-crooked.com/a/402554cd925ae653
http://rextester.com/CVWYH10446
You can use getline with a custom delimiter

http://www.cplusplus.com/reference/istream/istream/getline/
...or just use getline()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

int main()
{
    const std::string str = "112301900|501000200|111111|2222222|3333333|4444|55555|720302130" ;

    std::vector <std::string> tokens;
    // Convert str → tokens
    {
        std::string s;
        std::istringstream ss{ str };
        while (getline( ss, s, '|' )) tokens.emplace_back( s );
    }

    // Print tokens
    {
        const char* comma = "";
        for (const auto& tok : tokens)
            std::cout << std::exchange( comma, ", " ) << tok;
        std::cout << "\n";
    }
}
Rather depends what your data is between the pipes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;


template <class T> vector<T> splitString( string input )    // ASSUMES NO SIGNIFICANT SPACES
{                                                           //    BETWEEN PIPES
   vector<T> result;
   T value;

   replace( input.begin(), input.end(), '|', ' ' );
   stringstream ss( input );
   while ( ss >> value ) result.push_back( value );
   return result;
}


template <class T> ostream & operator << ( ostream &strm, vector<T> V ) { for ( auto e : V ) cout << e << " "; return strm; }


int main()
{
   string input = "112301900|501000200|720302130";

   vector<int> I = splitString<int>( input );
   cout << "Integers: " << I << endl;

   vector<string> S = splitString<string>( input );
   cout << "Strings:  " << S << endl;
}


Integers: 112301900 501000200 720302130 
Strings:  112301900 501000200 720302130 



Seamon17 wrote:
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.

Could you explain this, please.
Last edited on
Topic archived. No new replies allowed.