wild output from const char array

I have data that is coming into my buffer via popen (process data, not a file). Every seven records is a new set [0-6]. I am trying to 'print out the array line/element value' and 'change the value of element [2] to 0', but my loop appears to be looping through every character and not just every line?

code:

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

}
}
existing output:

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: .
expected output:

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!
I don't see where you're making sure you grab full lines at a time.

Also, we need to see the source to order.php to help further, otherwise there is no way to know what it outputs.
Last edited on
Each time through the outer loop you:
- read a line into buff
- point cstr2 to buff. So now it points to the line also.

Then in the inner loop you
- first time through the loop you print the first character in the line
- second time you print the second character in the line
- third time you check if the line is "Buy"

Topic archived. No new replies allowed.