我有数据通过popen进入我的缓冲区(进程数据,而不是文件)。 每七个记录是一个新的集[0-6]。 我试图打印出数组行/元素值,但我的循环似乎是循环遍历每个字符而不仅仅是每一行?
代码:
char* Data(){
char buff[BUFSIZ];
FILE *fp = popen("php order.php 155", "r");
std::string::size_type sz;
while(fgets(buff, sizeof buff, fp) != NULL)
{
const char * cstr2 = buff;
for(int i = 0; i < 6; ++i){
if(i == 0){
std::cout << "value 1: " << cstr2[0] << endl;
}
if(i == 1){
std::cout << "value 2: " << cstr2[1] << endl;
}
if(i == 2){
if (!strcmp(cstr2, "Buy\n")) {
printf("Found String!\n");
}
}
}
}
}
现有产出:
value 1: 1
value 2: 9
value 1: 2
value 2: 0
value 1: B
value 2: u
Found String!
value 1: 0
value 2: .
value 1: 1
value 2: 9
value 1: 1
value 2: 9
value 1: 0
value 2: .
value 1: 1
value 2: 9
value 1: 2
value 2: 0
value 1: B
value 2: u
Found String!
value 1: 0
value 2: .
value 1: 0
value 2: .
value 1: 0
value 2: .
value 1: 0
value 2: .
value 1: 1
value 2: 9
value 1: 2
value 2: 0
value 1: B
value 2: u
Found String!
value 1: 0
value 2: .
value 1: 0
value 2: .
value 1: 0
value 2: .
value 1: 0
value 2: .
value 1: 1
value 2: 9
value 1: 2
value 2: 0
value 1: B
value 2: u
Found String!
value 1: 0
value 2: .
value 1: 0
value 2: .
value 1: 0
value 2: .
value 1: 0
value 2: .
预期产出:
199729173
2014-11-16 10:09:34
Found String!
198397652
2014-11-14 15:10:10
Found String!
198397685
2014-11-14 15:10:13
Found String!
198398295
2014-11-14 15:11:14
Found String!
传入数据: inbound data
如何更正我的循环以在if语句中显示const char数组中的各个行/元素,类似于上面的代码?