I have inbound buffer data from a script that I need the key => 'value' to so that I can run a math equation against it (yes, I know I need to convert to int). Since I am sure the data is string, I trying to run a pattern match against it. I see the inbound data, but I never get a positive match.
code:
int getmyData()
{
char key[] = "total";
char buff[BUFSIZ];
FILE *fp = popen("php getMyorders.php 155", "r");
while (fgets( buff, BUFSIZ, fp)){
printf("%s", buff);
//if (strstr(key, buff) == buff) {
if (!memcmp(key, buff, sizeof(key) - 1)) {
std::cout << "Match "<< std::endl;
}
}
}
data output from print_f():
array(2) {
["success"]=>
string(1) "1"
["return"]=>
array(3) {
[0]=>
array(7) {
["orderid"]=>
string(9) "198397652"
["created"]=>
string(19) "2014-11-14 15:10:10"
["ordertype"]=>
string(3) "Buy"
["price"]=>
string(10) "0.00517290"
["quantity"]=>
string(10) "0.00100000"
["orig_quantity"]=>
string(10) "0.00100000"
["total"]=>
string(10) "0.00000517"
}
[1]=>
array(7) {
["orderid"]=>
string(9) "198397685"
["created"]=>
string(19) "2014-11-14 15:10:13"
["ordertype"]=>
string(3) "Buy"
["price"]=>
string(10) "0.00517290"
["quantity"]=>
string(10) "0.00100000"
["orig_quantity"]=>
string(10) "0.00100000"
["total"]=>
string(10) "0.00000517"
}
[2]=>
array(7) {
["orderid"]=>
string(9) "198398295"
["created"]=>
string(19) "2014-11-14 15:11:14"
["ordertype"]=>
string(3) "Buy"
["price"]=>
string(10) "0.00517290"
["quantity"]=>
string(10) "0.00100000"
["orig_quantity"]=>
string(10) "0.00100000"
["total"]=>
string(10) "0.00000517"
}
}
}
How would I get to ["total"] and add the # 3 to it? ["total"]+3?
You are matching just the first 5 bytes of
buff
for"total"
, instead of actually searching. If your buffer does not contain any nulls, the function you want to use is strstr:If your buffer can contain nulls, then you need to write a function that would be called memstr, which is pretty simple: just try to find it at every point:
whose usage in your case would be: