Jun 5, 2014 at 6:19am Jun 5, 2014 at 6:19am UTC
with respect to below string, i need to split it and store the values accordingly as below,
P2P-DEVICE-FOUND fa:7b:7a:42:02:13 p2p_dev_addr=fa:7b:7a:42:02:13
pri_dev_type=1-0050F204-1 name='p2p-TEST1' config_methods=0x188 dev_capab=0x27 group_capab=0x0 wfd_dev_info=000006015d022a0032
dev_addr = fa:7b:7a:42:02:13
dev_type = 1-0050F204-1
dev_name = p2p-TEST1
config_method = 0x188
dev_capab = 0x27
group_capab = 0x0
dev_info = 000006015d022a0032
Please do help me in splitting the above string. I am confused about how to split it as above and store. Please help me. I am new to c++
Jun 5, 2014 at 7:03am Jun 5, 2014 at 7:03am UTC
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
#include <vector>
#include <string>
#include <sstream>
std::vector<std::string> devSplit(const std::string &str) {
std::istringstream iss(str);
std::vector<std::string> vect;
std::string word;
static std::vector<std::string> values =
{
"dev_addr = " ,
"dev_type = " ,
"dev_name = " ,
"config_methods = " ,
"dev_capab = " ,
"group_capab = " ,
"dev_info = "
};
for (int v = 0; std::getline(iss, word, '=' ); v++) {
iss >> word;
vect.push_back(values[v] + word);
}
return vect;
}
http://coliru.stacked-crooked.com/a/ca1e1dc5241bdf31
Last edited on Jun 5, 2014 at 7:03am Jun 5, 2014 at 7:03am UTC
Jun 5, 2014 at 8:45am Jun 5, 2014 at 8:45am UTC
actually this is the string :
P2P-DEVICE-FOUND fa:7b:7a:42:02:13 p2p_dev_addr=fa:7b:7a:42:02:13
pri_dev_type=1-0050F204-1 name='p2p-TEST1' config_methods=0x188 dev_capab=0x27 group_capab=0x0 wfd_dev_info=000006015d022a0032
and i have below variables stored in a class called wifip2pdevice,
dev_addr
dev_type
dev_name
config_method
dev_capab
group_capab
dev_info
I have to split the above string and store the values alone in the above mentioned variables
Last edited on Jun 5, 2014 at 8:46am Jun 5, 2014 at 8:46am UTC
Jul 14, 2014 at 5:21am Jul 14, 2014 at 5:21am UTC
Hi Smac89,
I have executed your program in codeblocks. I got the following errors,
Could you please help me out in resolving the errors.
G:\C practicing\SplittingStrgs.cpp||In function 'std::vector<std::basic_string<char> > devSplit(const string&)':|
G:\C practicing\SplittingStrgs.cpp|20|error: in C++98 'values' must be initialized by constructor, not by '{...}'|
G:\C practicing\SplittingStrgs.cpp|20|error: could not convert '{"dev_addr = ", "dev_type = ", "dev_name = ", "config_methods = ", "dev_capab = ", "group_capab = ", "dev_info = "}' from '<brace-enclosed initializer list>' to 'std::vector<std::basic_string<char> >'|
G:\C practicing\SplittingStrgs.cpp||In function 'int main()':|
G:\C practicing\SplittingStrgs.cpp|33|error: range-based 'for' loops are not allowed in C++98 mode|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|
Thanks in advance.
Jul 14, 2014 at 6:06am Jul 14, 2014 at 6:06am UTC
go to project / build options then check the -std=c++11 check box
Jul 14, 2014 at 6:12am Jul 14, 2014 at 6:12am UTC
"Build options.." is in disable mode