-
why fread does not work?
Hi~ every one~ i have a piece of code here, But it does no work for the fread function. Can any one help me to figure it out ? thank you~(in the 50th line)
Code:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4
5 typedef struct _HEADER {
6
7 int num1;
8 int num2;
9 int num3;
10 int num4;
11
12 }HEADER;
13
14
15 void readheader(HEADER *,FILE *);
16 void writeheader(HEADER *,FILE *);
17 short**** construct_4D(int dimension1, int dimension2, int dimension3, int dimension4);
18
19 int main(int argc,char *argv[])
20 {
21
22
23
24 int i,j;
25 HEADER header;
26 short ****datatable;
27
28 datatable = (short ****)construct_4D(2,2,3,4);
29
30 /*if(argc !=2){
31 printf("Cannot open the file for read\n");
32 }*/
33
34
35 FILE *fp1=fopen(argv[1],"r");
36 FILE *fp2=fopen("newdata.dat","w");
37
38 printf("The processing file is %s \n",argv[1]);
39
40 for (i=0;i<2;i++){
41 for(j=0; j<2; j++){
42 datatable[i][j] = (short **)construct_4D(2,2,3,4);
43 }
44 }
45
46
47
48 readheader(&header,fp1);
49
50 for(i=0; i<2 ;i++){
51 for (j=0; j<4; j++)fread(&datatable[i][j][0][0],sizeof(short),6,fp1);
52 }
53 fclose( fp1 );
54
55 if ( fclose( fp1 ) == 0 )
56
57 {
58 printf("Success to close input file\n");
59 }
60
61 writeheader(&header,fp2);
62
63 printf("Can we get here?\n");
64
65
66 if(ferror(fp2)) {
67 printf("File2 Error\n");
68 exit(1);
69 }
70 if ( fclose( fp2 ) == 0 )
71 { printf("Success to close output file\n");
72 }
73 return(0);
74 }
75
76
77
78
79 void readheader(HEADER *header,FILE *fp1)
80 {
81 printf("Here is the readheader function\n");
82 fread(&(header->num1),sizeof(int),1,fp1);
83 fread(&(header->num2),sizeof(header->num2),1,fp1);
84 fread(&(header->num3),sizeof(int),1,fp1);
85 fread(&(header->num4),sizeof(int),1,fp1);
86 }
87
88 short**** construct_4D(int dimension1, int dimension2, int dimension3, int dimension4){
89 short**** result;
90 int i1, i2, i3;
91 result = (short****)malloc(sizeof(short***)*dimension1);
92 for (i1 = 0; i1 < dimension1; i1++){
93 result[i1] = (short***)malloc(sizeof(short**)*dimension2);
94 for (i2 = 0; i2 < dimension2; i2++){
95 result[i1][i2] = (short**)malloc(sizeof(short*)*dimension3);
96 for (i3 = 0; i3 < dimension3; i3++){
97 result[i1][i2][i3] = (short*)malloc(sizeof(short)*dimension4);
98 }
99 }
100 }
101 return result;
102 }
-
Do the fopen-s succeed? You never check.
-
-
So explain not working. What happens? Crash? Bad results?
The bad thing occurs at line 51?
Also, this code is bad, if not altogether wrong:
datatable[i][j] = (short **)construct_4D(2,2,3,4);
You are allocating a 4D-array, yet truncating it to a 2D-array.
-
1. remove all those casts of malloc, see the FAQ.
> datatable[i][j] = (short **)construct_4D(2,2,3,4);
What???
You've already built a 4-D array, why are you doing it again?
> fread(&datatable[i][j][0][0],sizeof(short),6,fp1);
1. This assumes that the two inner dimensions are contiguous - they aren't.
Only the innermost array is contiguous.
2. Where did 6 come from?
I mean, from construct_4D(2,2,3,4); you use 2 and 4 (boogle) in the controlling for loop, so I suppose 6 comes from the remaining 2 and 3.
Basically, the array you're reading into is completely the wrong shape.