2D Array only storing final row information.

Goal:
The aim of this code is to read in a .BMP, check each pixel, if black record 0, if not record 1 into a 2D Array.

Problem:
After assigning values into the Array (Arr), future output of Arr shows all rows as the same values, which happen to match the final values read from .BMP
However, if I output values to screen (cout) or a .TXT file as I read them from the .BMP, I get what I expect. I have tried assigning values to Arr as I read values from .BMP or from the .TXT file and get same undesired results.

Question:
What am I doing wrong when assigning values to the Array? Or am I reading from the Array incorrectly?

System and disclaimers:
I am using Dev-C++ on a Windows 7 machine. I have limited experience with C++ but am trying to learn. I've worked through several tutorials on C++ and tried reading about .bmp's to get this code. I've tried several versions of reading .bmp's into arrays and this version is working best so I'd prefer recommendations to fix this instead of complete rewrites.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iterator>

using std::ios; using std::ofstream; using std::ifstream; using std::cout;

int main(int argc, char *argv[])
{
    #define BMP "earthling_up.bmp"
    #define TXT "Text.txt"
    //open *.txt file
    ofstream OutFile(TXT); 
    
    FILE* img = fopen(BMP, "rb");   //open the *.bmp file
    unsigned char header[54];
    fread(header, sizeof(unsigned char), 54, img); // read the 54-byte header

    // extract image height and width from header
    int width = *(int*)&header[18];
    int height = *(int*)&header[22];

    int row_padded = (width*3 +3) & (~3);
    int size = ((width*3 +3) & (~3)) * height;
    unsigned char* data = new unsigned char[size];

    fread(data, sizeof(unsigned char), size, img);
    for (int i=0; i < height; i++)
    {
        for (int j=0; j < width*3; j+=3)
        {
            if ((data[i*row_padded+j]==0) && (data[i*row_padded+j+1]==0) && (data[i*row_padded+j+2]==0))
            {
                OutFile<<0<<" ";
            }
            else 
            {
                OutFile<<1<<" ";
            }
        }
        OutFile<<"\n";
    }
    OutFile.close();  // close *.txt file
    ifstream InFile(TXT); //open *.txt file
    int Number=2;
    int Arr[width][height];
    for (int i=0; i<height; i++)
    {
        for (int j=0; j<width; j++)
        {
            InFile>>Number;
            cout<<Number;
            Arr[i][j]=Number;
        }
        cout<<"\n";
    }
    
    cout<<"\n";
    for (int i=0; i<height; i++)
    {
        for (int j=0; j<width; j++)
        {
            cout<<Arr[i][j];
        }
        cout<<"\n";
    }
    
    InFile.close(); //close the *.txt file
    fclose(img); //close the *.bmp file    
    system("pause");
    delete data;
    return EXIT_SUCCESS;
}


Thank you for any help.
VLA's are not legal in standard C++, although gcc provides an extension (enabled by default) that allows them. Know that this code is not going to work with another compiler, or with your current one if you change some settings.

Perhaps you could show up us the code that isn't working. It's hard to say what's being done wrong when one can't see what's being done.
An example of how the code wasn't working as I was expecting:
Direct output to screen, or to .TXT file, showed:
000111000
011001011
001101100
000111000
001110000

Whereas assigning values to the array and then outputting the array provided:
001110000
001110000
001110000
001110000
001110000

Now that I know I can't use VLA's, I have a work around for this project. I know that all of my .BMP's will be no more than a certain size, say 100x100. Not very large images. As such, I can define my array as Arr[100][100], set all values to 0, and then read the image into that array. (Making this change into the above code gets me the results I expected).

For future projects, or if I start working with larger images, then I'll find a better way to handle this.

Thank you.
Topic archived. No new replies allowed.