Could someone check this code for me? I want to read a .dat file as a matrix and then print this matrix on screen.

with the following code, I want to read a .dat file as a matrix and then print this matrix on screen. But it just show 34343434343434 on my screen, my dat file is coming from a digital image file.

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
  #include<conio.h>
#include<stdio.h>

using namespace std;

int main()

{
    short i,j;

    short **pnumarray;
    pnumarray=new short *[1350];


    for(int i=0;i<1350;i++){
        pnumarray[i]=new short [1040];

    }


        short snum;

        FILE*fp;

        fp=fopen("20140512test.dat","rb+");

        if(fp==NULL)
      {printf("the file can not be open");
        }
        else
        {
                for (i=0;i<1349;i++)
        {
            for (j=0;j<1039;j++)
            {
                fscanf(fp,"%d",&snum);
                pnumarray[i][j]=snum;
            }
        }
        fclose(fp);
    }
    for (i=0;i<1349;i++)
    {
    for (j=0;j<1039;j++)
    {
    printf("%d",pnumarray[i][j]);

    }
    }

    getch();

    for(unsigned i = 0; i < 1350; ++i) {
                         delete [] pnumarray[i];
                 }
                 delete [] pnumarray;

    return 0;
    }
Last edited on
snum is short (~16bit) while fscanf(fp,"%d", requires int (~32 bit)
Topic archived. No new replies allowed.