1

I am trying to read values from a ppm image file and i get "Run-Time Check Failure #2 - Stack around the variable 'numbers' was corrupted" error. I am new to c++ and programming in general and i dont understand what is the problem, can someone help?

I dont know if it is important, but the code i have written in main i will later transfer in a method that will perform this task( i will do all the necessery adjustments), is a similar error going to occur inside the method?

(the code is written in visual studio in debug mode)

int main(int argc, char *argv[]) {

ifstream imagefile(argv[1], ios::binary);

if (!imagefile.is_open()) {
printf("error opening");
return 0;
}

string values[4];
int x;
for (int i = 0; i < 4; i++) {
    string str;
    char c = imagefile.get();

    while (!isspace(static_cast<unsigned char>(c))) {   
        str = str + c;
        c = imagefile.get();
        if (str.size() > 10000) {
            cout << "wrong" << endl;    //for testing
            goto stop;
        }
    }
    x=i;
    cout << x << endl;  //for testing
    values[i] = str;
    cout << str << endl;    //for testing
}   
stop:
if (!values[0].compare("P6") == 0 || x!=3) {
    cout << "wrong format" << endl;
    system("pause");
    return 0;
}

int numbers[3];
for (int i= 1; i <4; i++) {
    stringstream str;       //to convert string to integer
    str << values[i];
    int number;
    str >> number;
    numbers[i] = number;
    cout << numbers[i] +1<< endl;   //for testing
}

int start = imagefile.tellg();
imagefile.seekg(0, ios::end);
int end = imagefile.tellg();
int size = end - start;
imagefile.seekg(start, ios::beg);
float *pixels = new float[size];
for (int i = 0; i < size; i++) {
    int c = imagefile.get();
    float f = c / 255.0;
    pixels[i] = f;
}

system("pause");

return 0;

}

JDoe
  • 27
  • 7

0 Answers0