Error in reading a byte from a file.

Hello,

I am trying to read bytes from a file but it's just not working...
My program reads graphic data from a file and then creates a png image.

If i manually specify the coordinates instead of reading them from a file it works fine.

However reading the byte from the file does not work.

Any ideas?
Thanks in advance.




For example.
This works...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (p >= 33 && p < 40)
            {
                x_coord = (0x7c - x_coord_Large) * 4;
                y_coord = 0x7e - y_coord_Large;

                if (p == 32) { c = 256 * (y_coord + 7) * 4 + x_coord; }
                if (p == 33) { c = 256 * (y_coord + 6) * 4 + x_coord; }
                if (p == 34) { c = 256 * (y_coord + 5) * 4 + x_coord; }
                if (p == 35) { c = 256 * (y_coord + 4) * 4 + x_coord; }
                if (p == 36) { c = 256 * (y_coord + 3) * 4 + x_coord; }
                if (p == 37) { c = 256 * (y_coord + 2) * 4 + x_coord; }
                if (p == 38) { c = 256 * (y_coord + 1) * 4 + x_coord; }
                if (p == 39) { c = 256 * (y_coord) * 4 + x_coord; }
            }




But this does not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 if (p >= 33 && p < 40)
            {


                char oDataK[0x16000]; // Balloon buffer
                ifstream inFileK;
                inFileK.open("C:\\Users\\Chris\\Desktop\\file.dat", ios::binary); // Note. this opens a binary streem. This is needed. cuzz the program was using 0x1a values as a Substitute AscII charter which halts the program/Using it as End Of File.
                inFileK.seekg(0x2B6D78, 1); // Location of the header.
                inFileK.read(oDataK, 0x16000);

                x_coord = (oDataK[13] - x_coord_Large) * 4;
                y_coord = oDataK[14] - y_coord_Large;

                if (p == 32) { c = 256 * (y_coord + 7) * 4 + x_coord; }
                if (p == 33) { c = 256 * (y_coord + 6) * 4 + x_coord; }
                if (p == 34) { c = 256 * (y_coord + 5) * 4 + x_coord; }
                if (p == 35) { c = 256 * (y_coord + 4) * 4 + x_coord; }
                if (p == 36) { c = 256 * (y_coord + 3) * 4 + x_coord; }
                if (p == 37) { c = 256 * (y_coord + 2) * 4 + x_coord; }
                if (p == 38) { c = 256 * (y_coord + 1) * 4 + x_coord; }
                if (p == 39) { c = 256 * (y_coord) * 4 + x_coord; }
            }









Last edited on
First thing is to check that your file is being opened correctly.

After line 7:
1
2
3
4
5
if (!inFileK)
{
    cout << "Error opening file!\n";
    return ...;
}


seekg's second parameter is an enum. You should use ios::beg instead of 1.

0x2B6D78
How do you know this magic number is correct?
Last edited on
Thanks for your reply.

I inserted that and it doesn't throw an error.

that number is an address in my file. I know it's correct as i checked in a Hex viewer.
To reiterate, your code does not compile on a modern compiler.
main.cpp: In function 'int main()':
main.cpp:8:20: error: invalid conversion from 'int' to 'std::ios_base::seekdir {aka std::_Ios_Seekdir}' [-fpermissive]
  inFileK.seekg(0x2B6D78, 1); // Location of the header.


On my compiler, std::ios::beg is 0, not 1.
Are you absolutely positive that ios::beg is 1 on your system? It's implementation-defined, which is why the enum should be used.
https://en.cppreference.com/w/cpp/io/ios_base/seekdir

inFileK.seekg(0x2B6D78, ios::beg); // Location of the header.

You may also want to consider doing other sanity check tests, for example, reading the second byte of the file:
1
2
nFileK.seekg(1, ios::beg);
nFileK.read(oDataK, 1);

Check that it matches what your hex viewer shows.

Edit: ios::cur would also work for your code... which does happen to have a value of 1 on my compiler. So perhaps that part of your code is not the issue, but you should be using the ios enums.
Last edited on
Does 'endiness' have to be considered here?

What values are you getting for oDataK[13] and oDataK[14]. If not as expected (0x7c, 0x7e), where do these consecutive values occur in the data file using the hex viewer (is this configured properly)?
Last edited on
Here is a screenshot of my Hex editor. They are highlighted in yellow.

https://i.imgur.com/0sUA4Ds.png

I am trying to read these 2 bytes individually. X and Y.
From the picture, the address of the yellow bytes is 0x2B6D84 which is only 12 10 (not 13) bytes from the start of the header at 0x2B6D78
Last edited on
A little debugging may point out something obvious. Specifically, I'm unconvinced that your method of reading in the file and going through the char array is actually doing the equivalent of your first code:

1
2
3
4
5
6
7
//Are you sure you didn't seek past your intended location?
inFileK.seekg(0x2B6D78, 1); // Location of the header.
inFileK.read(oDataK, 0x16000);

//Why [13] and [14]?
x_coord = (oDataK[13] - x_coord_Large) * 4;
y_coord = oDataK[14] - y_coord_Large;


If x_coord and y_coord are different values, the issue has to come from the way you're parsing the file. I recommend outputting the values, seeing where the come from within the file, and seeing if there's a margin of error in where you thought the values were vs where you ended up.
If you are seeking to 0x2B6D78, then x_coord (0x7c) is offset 10 and y-coord (0x7E) is offset 11.

1
2
3
4
5
inFileK.seekg(0x2B6D78, ios::beg); // Location of the header.
inFileK.read(oDataK, 0x16000);

x_coord = (oDataK[10] - x_coord_Large) * 4;
y_coord = oDataK[11] - y_coord_Large;



Location  off  val
0x2B6D78  00   0x01
0x2B6D79  01   0x02
0x2B6D7A  02   0x02
0x2B6D7B  03   0x00
0x2B6D7C  04   0x00
0x2B6D7D  05   0x04
0x2B6D7E  06   0x10
0x2B6D7F  07   0x02
0x2B6D80  08   0x78
0x2B6D81  09   0x6E
0x2B6D82  10   0x7C
0x2B6D83  11   0x7E 

Last edited on

So I have it partially figured out.
However I am Having an issue were the previous blocks of code don't work when I add an if statement after the previous block.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
for (int p = 0, t = 0; p < 48; p++, t++)
        {
            // Assign each row of the tiles a value from the image buffer. p is the row offset. c is the location within the buffer.

            // 16 x 16 tile groups

            // Top Left
            // 256 is the number of pixels per row in the png image then multiplied by the row number. Then this is multiplied by 4 because there are 4 bytes per pixel for example Red, Green, Blue and Alpha = 4.

            if (p >= 0 && p < 8)
            {

                x_coord = (oDataK[Header + 0] - x_coord_Large) * 4;
                y_coord = oDataK[Header + 1] - y_coord_Large;


                if (X > 0)
                {
                    x_coord = (0x78 - x_coord_Large) * 4;
                    y_coord = 0x6e - y_coord_Large;

                }



                if (p == 0) { c = 256 * (y_coord + 7) * 4 + x_coord; }
                if (p == 1) { c = 256 * (y_coord + 6) * 4 + x_coord; }
                if (p == 2) { c = 256 * (y_coord + 5) * 4 + x_coord; }
                if (p == 3) { c = 256 * (y_coord + 4) * 4 + x_coord; }
                if (p == 4) { c = 256 * (y_coord + 3) * 4 + x_coord; }
                if (p == 5) { c = 256 * (y_coord + 2) * 4 + x_coord; }
                if (p == 6) { c = 256 * (y_coord + 1) * 4 + x_coord; }
                if (p == 7) { c = 256 * (y_coord) * 4 + x_coord; }
            }

            // Top Right
            // 32 is the tile offset. 8 pixels to the next tile multiplied by 4 = 32. 4 is the number of bytes per pixel.
            if (p == 8) { c = 256 * (y_coord + 7) * 4 + x_coord + offset; }
            if (p == 9) { c = 256 * (y_coord + 6) * 4 + x_coord + offset; ; }
            if (p == 10) { c = 256 * (y_coord + 5) * 4 + x_coord + offset; ; }
            if (p == 11) { c = 256 * (y_coord + 4) * 4 + x_coord + offset; ; }
            if (p == 12) { c = 256 * (y_coord + 3) * 4 + x_coord + offset; ; }
            if (p == 13) { c = 256 * (y_coord + 2) * 4 + x_coord + offset; ; }
            if (p == 14) { c = 256 * (y_coord + 1) * 4 + x_coord + offset; ; }
            if (p == 15) { c = 256 * (y_coord) * 4 + x_coord + offset; ; }


            // Bottom Left
            if (p == 16) { c = 256 * (y_coord + 15) * 4 + x_coord; }
            if (p == 17) { c = 256 * (y_coord + 14) * 4 + x_coord; }
            if (p == 18) { c = 256 * (y_coord + 13) * 4 + x_coord; }
            if (p == 19) { c = 256 * (y_coord + 12) * 4 + x_coord; }
            if (p == 20) { c = 256 * (y_coord + 11) * 4 + x_coord; }
            if (p == 21) { c = 256 * (y_coord + 10) * 4 + x_coord; }
            if (p == 22) { c = 256 * (y_coord + 9) * 4 + x_coord; }
            if (p == 23) { c = 256 * (y_coord + 8) * 4 + x_coord; }

            // Bottom Right
            if (p == 24) { c = 256 * (y_coord + 15) * 4 + x_coord + offset;; }
            if (p == 25) { c = 256 * (y_coord + 14) * 4 + x_coord + offset;; }
            if (p == 26) { c = 256 * (y_coord + 13) * 4 + x_coord + offset;; }
            if (p == 27) { c = 256 * (y_coord + 12) * 4 + x_coord + offset;; }
            if (p == 28) { c = 256 * (y_coord + 11) * 4 + x_coord + offset;; }
            if (p == 29) { c = 256 * (y_coord + 10) * 4 + x_coord + offset;; }
            if (p == 30) { c = 256 * (y_coord + 9) * 4 + x_coord + offset;; }
            if (p == 31) { c = 256 * (y_coord + 8) * 4 + x_coord + offset;; }



            // 8 x 8 tiles
            // #1
            if (p >= 33 && p < 40)
            {
                // For Last Image
                    x_coord = (oData[10] - x_coord_Large) * 4;
                    y_coord = oData[11] - y_coord_Large;

                    if (p == 32) { c = 256 * (y_coord + 7) * 4 + x_coord; }
                    if (p == 33) { c = 256 * (y_coord + 6) * 4 + x_coord; }
                    if (p == 34) { c = 256 * (y_coord + 5) * 4 + x_coord; }
                    if (p == 35) { c = 256 * (y_coord + 4) * 4 + x_coord; }
                    if (p == 36) { c = 256 * (y_coord + 3) * 4 + x_coord; }
                    if (p == 37) { c = 256 * (y_coord + 2) * 4 + x_coord; }
                    if (p == 38) { c = 256 * (y_coord + 1) * 4 + x_coord; }
                    if (p == 39) { c = 256 * (y_coord) * 4 + x_coord; }

        /////////////////////// Adding the below makes the previous block not to work.
                    if(X == 0)
                    {
                        // For First Image
                        x_coord = (oDataK[Header + 2] - x_coord_Large) * 4;
                        y_coord = oDataK[Header + 3] - y_coord_Large;

                        if (p == 32) { c = 256 * (y_coord + 7) * 4 + x_coord; }
                        if (p == 33) { c = 256 * (y_coord + 6) * 4 + x_coord; }
                        if (p == 34) { c = 256 * (y_coord + 5) * 4 + x_coord; }
                        if (p == 35) { c = 256 * (y_coord + 4) * 4 + x_coord; }
                        if (p == 36) { c = 256 * (y_coord + 3) * 4 + x_coord; }
                        if (p == 37) { c = 256 * (y_coord + 2) * 4 + x_coord; }
                        if (p == 38) { c = 256 * (y_coord + 1) * 4 + x_coord; }
                        if (p == 39) { c = 256 * (y_coord) * 4 + x_coord; }

                    }

            }

            // #2
            if (p >= 40)
            {
                    // For Last image.
                    x_coord = (oData[12] - x_coord_Large) * 4;
                    y_coord = oData[13] - y_coord_Large;

                    if (p == 40) { c = 256 * (y_coord + 7) * 4 + x_coord; }
                    if (p == 41) { c = 256 * (y_coord + 6) * 4 + x_coord; }
                    if (p == 42) { c = 256 * (y_coord + 5) * 4 + x_coord; }
                    if (p == 43) { c = 256 * (y_coord + 4) * 4 + x_coord; }
                    if (p == 44) { c = 256 * (y_coord + 3) * 4 + x_coord; }
                    if (p == 45) { c = 256 * (y_coord + 2) * 4 + x_coord; }
                    if (p == 46) { c = 256 * (y_coord + 1) * 4 + x_coord; }
                    if (p == 47) { c = 256 * (y_coord) * 4 + x_coord; }
                
          /////////////////////// Adding the below makes the previous block not to work.

                    // For First Image
                   if(X == 0)
                    {
                        x_coord = (oDataK[Header + 5] - x_coord_Large) * 4;
                        y_coord = oDataK[Header + 4] - y_coord_Large;

                        if (p == 40) { c = 256 * (y_coord + 7) * 4 + x_coord; }
                        if (p == 41) { c = 256 * (y_coord + 6) * 4 + x_coord; }
                        if (p == 42) { c = 256 * (y_coord + 5) * 4 + x_coord; }
                        if (p == 43) { c = 256 * (y_coord + 4) * 4 + x_coord; }
                        if (p == 44) { c = 256 * (y_coord + 3) * 4 + x_coord; }
                        if (p == 45) { c = 256 * (y_coord + 2) * 4 + x_coord; }
                        if (p == 46) { c = 256 * (y_coord + 1) * 4 + x_coord; }
                        if (p == 47) { c = 256 * (y_coord) * 4 + x_coord; }
                    }



            }
Last edited on
You should try stepping through your code line by line with a debugger so you can tell in exactly what way it is going wrong.
Not sure what I did but its working now. It might have been to do with conflicting ifstream's or in the wrong location in the code?


Thanks!
Topic archived. No new replies allowed.