0

キー => 'value' が必要なスクリプトからのインバウンド バッファ データがあるので、それに対して数式を実行できます (はい、int に変換する必要があることはわかっています)。データが文字列であることは確かなので、それに対してパターン マッチを実行しようとしています。インバウンド データが表示されますが、正の一致が得られません。

コード:

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;
                }

        }
}

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"
    }
  }   
}

["total"] に 3 を追加するにはどうすればよいですか? ["合計"]+3?

4

1 に答える 1

0

実際に検索するのではなく、buffforの最初の 5 バイトだけを一致させています。"total"バッファーにヌルが含まれていない場合、使用する関数はstrstrです。

while (fgets( buff, BUFSIZ, fp)) {
    const char* total = strstr(buff, key);
    if (total) {
        // found our total, which should point
        // ["total"] =>
        //   ^
        //   here
    }
}

バッファーに null を含めることができる場合は、memstr と呼ばれる関数を作成する必要があります。これは非常に簡単です。すべてのポイントで見つけてみてください。

const char* memstr(const char* str, size_t str_size, 
                   const char* target, size_t target_size) {

    for (size_t i = 0; i != str_size - target_size; ++i) {
        if (!memcmp(str + i, target, target_size)) {
            return str + i;
        }
    }

    return NULL;
}

あなたの場合の使用法は次のとおりです。

const char* total = memstr(buff, BUFSIZ, key, sizeof(key) - 1);
于 2014-11-14T22:29:31.820 に答える