0

im a newbie on programming, and i need to show a binary file in my program in the first phase.

with the help of my teacher, i was able to do that, but when i was working late in the code i change somethings that i couldn't get back now :(

this is the code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
    FILE *f;
    int a = 0, b = 0, c = 0, d = 0;
    float cinzentos = 0, mlinha = 0, ppixeis = 0;
    unsigned char matriz[28][28][500];

    f = fopen("dados1.bin", "rb");

    if (f != NULL) {
        while (!feof(f)) {
            b++;

            if (b == 28) {
                printf(" soma: %f ", mlinha);
                printf(" cizentos: %f ", mlinha / 28);
                printf("\n");
                b = 0;
                a++;
                mlinha = 0;
                printf("%d %d %d", a, b, c);
            }

            if (a == 28) {
                a = 0;
                c++;
                ppixeis = (ppixeis / 28 * 28) * 0.1;
                printf("media de pixeis: %f\n", ppixeis);
                printf("\n---------------\n");
            }

            matriz[a][b][c] = fgetc(f);

            if (matriz[a][b][c] != 0) {
                printf("1");
                mlinha = mlinha + matriz[a][b][c];
                ppixeis++;
                fputc(matriz[a][b][c], p);
            } else {
                printf("0");
            }
        }
    }

    fclose(f);

    return 0;
}

I think, that's is because the file opening, but im in looping trying to put this back together , need help

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • 1
    I would have to simply guess that's you're trying to access `matriz` with an index that is larger than the array. Before every access to it, check `a`, `b`, and `c` to see if they're good values. – Steve Nov 04 '13 at 15:33
  • 3
    Also, learn and start using a version control system (like Git). If you check in your code in frequently, then make a change that breaks your program, you can go back to a working version, and even perform a difference between the working version and the current broken version and see *exactly* what's different. – Steve Nov 04 '13 at 15:36
  • 2
    Also, while (!feof()) is always wrong, see http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – SirDarius Nov 04 '13 at 15:44
  • It would be easier to use `for (a = 0; a < 28; a++) { for (b = 0; b < 28; b++) { for (c = 0; c < 500; c++) { int v; if ((v = getc(f)) == EOF) { ...break out of input loops on error, or exit... } matriz[a][b][c] = v; } } }`. – Jonathan Leffler Nov 04 '13 at 16:00
  • it was about the var c < 500 check, that was missing, someone post that but deleted the post, thanks to that guy! also, thanks to the other persons who are giving guidelines to some lines that will give me problems in the future, i will take that too under observation of my code. – user2953064 Nov 04 '13 at 16:09

1 Answers1

0

It looks like your code is off by one

The

while (!feof(f)) {
            b++;

ensures that b will not start at 0 at the first read character

AndersK
  • 35,813
  • 6
  • 60
  • 86