Why not success write the data?

X

xiao

HI~ guys , I have a program here (Sorry it is very long about 240
lines.) It can read and write the header information successfully but
it cannot write the array successfully. I guess there is something
wrong with the Write2DArrayInt function there. Can any one henlp me
to find it out? Thank you~ (BTW : The printf function in line 114 and
139 implys that the data is right, but after that , in the new
generated file, the values are all zero.....:( )


1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5
6 typedef struct _GRIDDEF
7 {
8 double CentX;
9 double CentY;
10 double CentLat;
11 double CentLon;
12 double TlX;
13 double TlY;
14 double DeltaX;
15 double DeltaY;
16 long ProjectionID;
17 double ProjectionParameters[15];
18 long (*inv_trans[100])(double,double,double *, double *);
19 long (*for_trans[100])(double,double,double *, double *);
20 long MapZone;
21 long InDatum;
22 long stat;
23 int NumX;
24 int NumY;
25 } GRIDDEF;
26
27
28 short **Allocate2DInt(int rows,int columns);
29 float **Allocate2DFloat(int rows,int columns);
30 void Write2DArrayInt(short **Array, int Columns, int Rows,
FILE *fp);
31 void WriteHeader(GRIDDEF *, FILE *);
32 void free_2D_float(float **);
33 void free_2D_Int(short **);
34
35 int main()
36 {
37 GRIDDEF psgrid;
38
39 psgrid.MapZone = 0;
40 psgrid.InDatum = -1;
41 psgrid.ProjectionParameters[0] = 6371007.181;
42 psgrid.CentLat = -25.0;
43 psgrid.CentLon = 108;
44 psgrid.NumX = 800;
45 psgrid.NumY = 800;
46 psgrid.DeltaX = 1000.0;
47 psgrid.DeltaY = 1000.0;
48 psgrid.ProjectionID = 10;
49 54 float **latitude,**lontitude;
55 short
**clear,**tcumulus,**cowacloud,**cumulus,**wawacloud,**thickcicloud,**verysmallcumu,**cirrus,**solidwa,**thwacloud1,**cirrordeepcon,**stratu
s,**Cb;
56
57 FILE *in;
58 FILE *out;
59 FILE *lat,*lon;
60 lat =
fopen("MOD021KM.A2005243.0255.005.2008027115345.lat", "r");
61 lon =
fopen("MOD021KM.A2005243.0255.005.2008027115345.lon", "r");
62
in=fopen("MOD021KM.A2005243.0255.005.2008027115345.200","r");
63 out=fopen("classified.dat","w");
64
65 class=Allocate2DInt(nrows, ncolumns);
66 clear=Allocate2DInt(nrows, ncolumns);
67 tcumulus=Allocate2DInt(nrows, ncolumns);
68 cowacloud=Allocate2DInt(nrows, ncolumns);
69 cumulus=Allocate2DInt(nrows, ncolumns);
70 wawacloud=Allocate2DInt(nrows, ncolumns);
71 thickcicloud=Allocate2DInt(nrows, ncolumns);
72 verysmallcumu=Allocate2DInt(nrows, ncolumns);
73 cirrus=Allocate2DInt(nrows, ncolumns);
74 solidwa=Allocate2DInt(nrows, ncolumns);
75 thwacloud1=Allocate2DInt(nrows, ncolumns);
76 cirrordeepcon=Allocate2DInt(nrows, ncolumns);
77 stratus=Allocate2DInt(nrows, ncolumns);
78 Cb=Allocate2DInt(nrows, ncolumns);
79
80
81 if(in != NULL){
82 printf ("Opened file named successfully\n");
83 }
84 latitude=Allocate2DFloat(nrows, ncolumns);
85 lontitude=Allocate2DFloat(nrows, ncolumns);
86
87 for(i=0; i<nrows; i++){
88 for(j=0; j<ncolumns; j++){
89 latitude[j] = 0.0;
90 lontitude[j] = 0.0;
91 class[j]=0;
92 clear[j]=0;
93 tcumulus[j]=0;
94 cowacloud[j]=0;
95 cumulus[j]=0;
96 wawacloud[j]=0;
97 thickcicloud[j]=0;
98 verysmallcumu[j]=0;
99 cirrus[j]=0;
100 solidwa[j]=0;
101 cirrordeepcon[j]=0; 104 thwacloud1
[j]=0;
105 } 106 }
107 for(i=0; i<nrows; i++){
108 fread(latitude,sizeof(float),ncolumns,lat);
109
fread( lontitude,sizeof(float),ncolumns,lon);
110 fread(class,sizeof(short),ncolumns,in);
111 }
112 for(i=0;i<nrows;i++){
113 for(j=0;j<ncolumns;j++){
114 /* printf("The lat is %hd\t",class[j]);*/
115 }
116 }
117
118 for(i=0; i<nrows; i++){
119 for(j=0; j<ncolumns; j++){
120 /*printf("the data is %hd",class[j]); */
121 if (class[j]==1){ clear[j]=1;}else {clear[j]=0;}
122 if (class[j]==2){ tcumulus[j]=1;}else {tcumulus
[j]=0;}
123 if (class[j]==3){ cowacloud[j]=1;}else {cowacloud
[j]=0;}
124 if (class[j]==4){ cumulus[j]=1;}else {cumulus[j]=0;}
125 if (class[j]==5){ wawacloud[j]=1;}else {wawacloud
[j]=0;}
126 if (class[j]==6){ thickcicloud[j]=1;}else
{thickcicloud[j]=0;}
127 if (class[j]==7){ verysmallcumu[j]=1;}else
{verysmallcumu[j]=0;}
128 if (class[j]==8){ cirrus[j]=1;}else {cirrus[j]=0;}
129 if (class[j]==9){ solidwa[j]=1;}else {solidwa[j]=0;}
130 if (class[j]==10){ thwacloud1[j]=1;}else {thwacloud1
[j]=0;}
131 if (class[j]==11){cirrordeepcon[j]=1;}else
{cirrordeepcon[j]=0;}
132 if (class[j]==12){ stratus[j]=1;}else {stratus
[j]=0;}
133 if (class[j]==13){ Cb[j]=1;}else {Cb[j]=0;}
134 /* printf("The lat is %hd\t",tcumulus[j]);*/
135 }
136 }
137
138 for(i=0; i<10; i++){
139 for(j=0; j<10; j++){printf("The lat is %d
\t",cumulus[j]);
140 }}
141
142 WriteHeader(&psgrid, out);
143 :wq
144
145 Write2DArrayInt(tcumulus, ncolumns,nrows, out);
146 Write2DArrayInt(clear, ncolumns,nrows, out);
147 Write2DArrayInt(cowacloud, ncolumns,nrows, out);
148 Write2DArrayInt(cumulus, ncolumns,nrows, out);
149 Write2DArrayInt(wawacloud, ncolumns,nrows, out);
150 Write2DArrayInt(thickcicloud, ncolumns,nrows, out); 155
Write2DArrayInt(cirrordeepcon, ncolumns,nrows, out);
156 Write2DArrayInt(stratus, ncolumns,nrows, out);
157 Write2DArrayInt(Cb, ncolumns,nrows, out);
158
159 free_2D_Int(tcumulus);
160 free_2D_Int(clear);
161 free_2D_Int(cowacloud);
162 free_2D_Int(cumulus);
163 free_2D_Int(wawacloud);
164 free_2D_Int(thickcicloud);
165 free_2D_Int(verysmallcumu);
166 free_2D_Int(cirrus);
167 free_2D_Int(solidwa);
168 free_2D_Int(thwacloud1);
169 free_2D_Int(cirrordeepcon);
170 free_2D_Int(stratus);
171 free_2D_Int(Cb);
172
173
174 fclose(in);
175 fclose(out);
176 fclose(lat);
177 fclose(lon);
178
179 return(0);
180 }
181
182
183 void WriteHeader(GRIDDEF *psgrid, FILE *out)
184 {
185 fwrite(&(psgrid->CentLat),sizeof(double),1,out);
186 fwrite(&(psgrid->CentLon),sizeof(double),1,out);
187 fwrite(&(psgrid->DeltaX),sizeof(double),1,out);
188 fwrite(&(psgrid->DeltaY),sizeof(double),1,out);
189 fwrite(&(psgrid->ProjectionID),sizeof(long),1,out);
190 fwrite(psgrid->ProjectionParameters,sizeof(double),15,out);
191 fwrite(&(psgrid->MapZone),sizeof(long),1,out);
192 fwrite(&(psgrid->NumX),sizeof(int),1,out);
193 fwrite(&(psgrid->NumY),sizeof(int),1,out);
194 }
195
196
197 /* Function to create a 2D float array of pointers*/
198 short **Allocate2DInt(int rows,int columns) 206 return
pntr;
199 {
200 short **pntr;
201 int i;
202
203 pntr = (short **)malloc(sizeof(short *)*rows);
204 pntr[0] = (short *)malloc(sizeof(short )*rows*columns);
205 for(i=1; i<rows; i++)pntr = pntr[i-1]+columns;
206 return pntr;
207 }
208
209 float **Allocate2DFloat(int rows,int columns)
210 {
211 float **pntr;
212 int i;
213
214 pntr = (float **)malloc(sizeof(float *)*rows);
215 pntr[0] = (float *)malloc(sizeof(float )*rows*columns);
216 for(i=1; i<rows; i++)pntr = pntr[i-1]+columns;
217 return pntr;
218 }
219
220 void Write2DArrayInt(short **Array, int Columns, int Rows,
FILE *fp)
221 {
222
223 int i;
224
225 for(i=0; i<Rows; i++){
226 fwrite(Array, sizeof(short),Columns, fp);
227 }
228 /*fclose(fp);*/
229 }
230
231 void free_2D_float(float **pntr)
232 {
233
234 free(pntr[0]);
235
236 free(pntr);
237 }
238
239 void free_2D_Int(short **pntr)
240 {
241
242 free(pntr[0]);
243
244 free(pntr);
245 }
 
B

Barry Schwarz

HI~ guys , I have a program here (Sorry it is very long about 240
lines.) It can read and write the header information successfully but
it cannot write the array successfully. I guess there is something

What do you mean cannot write the array successfully. How do you
check the contents of the binary file? (You open it in text mode but
you write binary data to it.) What do you see that leads you to
believe there is a problem? Be specific. What are the contents? What
were you expecting?

You output file starts with four double, a long, fifteen double (of
which the last 14 contain indeterminate values), one long, and two
int. Are you sure you are looking for your array data in the correct
location.
wrong with the Write2DArrayInt function there. Can any one henlp me
to find it out? Thank you~ (BTW : The printf function in line 114 and
139 implys that the data is right, but after that , in the new
generated file, the values are all zero.....:( )

The printf on line 114 prints the elements of the array class. At no
time do you call Write2DArrayInt to write this array to the file.
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>

People interested in helping would probably like to compile your code.
Get rid of the irritating line numbers.
4
5
6 typedef struct _GRIDDEF
7 {
8 double CentX;
9 double CentY;
10 double CentLat;
11 double CentLon;
12 double TlX;
13 double TlY;
14 double DeltaX;
15 double DeltaY;
16 long ProjectionID;
17 double ProjectionParameters[15];
18 long (*inv_trans[100])(double,double,double *, double *);
19 long (*for_trans[100])(double,double,double *, double *);
20 long MapZone;
21 long InDatum;
22 long stat;
23 int NumX;
24 int NumY;
25 } GRIDDEF;
26
27
28 short **Allocate2DInt(int rows,int columns);

Why is a function that allocates a 2d array of short called
Allocate2DInt?
29 float **Allocate2DFloat(int rows,int columns);
30 void Write2DArrayInt(short **Array, int Columns, int Rows,
FILE *fp);
31 void WriteHeader(GRIDDEF *, FILE *);
32 void free_2D_float(float **);
33 void free_2D_Int(short **);
34
35 int main()
36 {
37 GRIDDEF psgrid;
38
39 psgrid.MapZone = 0;
40 psgrid.InDatum = -1;
41 psgrid.ProjectionParameters[0] = 6371007.181;
42 psgrid.CentLat = -25.0;

Interesting that for some doubles you feel the need to include the .0
in the value ...
43 psgrid.CentLon = 108;

.... while for others you don't. Consistency does help make your code
more readable.
44 psgrid.NumX = 800;
45 psgrid.NumY = 800;
46 psgrid.DeltaX = 1000.0;
47 psgrid.DeltaY = 1000.0;
48 psgrid.ProjectionID = 10;
49 54 float **latitude,**lontitude;

Obviously something is missing here. And how do we know it is not
related to your problem?
55 short
**clear,**tcumulus,**cowacloud,**cumulus,**wawacloud,**thickcicloud,**verysmallcumu,**cirrus,**solidwa,**thwacloud1,**cirrordeepcon,**stratu
s,**Cb;
56
57 FILE *in;
58 FILE *out;
59 FILE *lat,*lon;
60 lat =
fopen("MOD021KM.A2005243.0255.005.2008027115345.lat", "r");
61 lon =
fopen("MOD021KM.A2005243.0255.005.2008027115345.lon", "r");
62
in=fopen("MOD021KM.A2005243.0255.005.2008027115345.200","r");
63 out=fopen("classified.dat","w");
64
65 class=Allocate2DInt(nrows, ncolumns);

Where are nrows and ncolumns defined? What values do they contain?
66 clear=Allocate2DInt(nrows, ncolumns);
67 tcumulus=Allocate2DInt(nrows, ncolumns);
68 cowacloud=Allocate2DInt(nrows, ncolumns);
69 cumulus=Allocate2DInt(nrows, ncolumns);
70 wawacloud=Allocate2DInt(nrows, ncolumns);
71 thickcicloud=Allocate2DInt(nrows, ncolumns);
72 verysmallcumu=Allocate2DInt(nrows, ncolumns);
73 cirrus=Allocate2DInt(nrows, ncolumns);
74 solidwa=Allocate2DInt(nrows, ncolumns);
75 thwacloud1=Allocate2DInt(nrows, ncolumns);
76 cirrordeepcon=Allocate2DInt(nrows, ncolumns);
77 stratus=Allocate2DInt(nrows, ncolumns);
78 Cb=Allocate2DInt(nrows, ncolumns);
79
80
81 if(in != NULL){
82 printf ("Opened file named successfully\n");
83 }

What do you do if in is NULL?
84 latitude=Allocate2DFloat(nrows, ncolumns);
85 lontitude=Allocate2DFloat(nrows, ncolumns);
86
87 for(i=0; i<nrows; i++){
88 for(j=0; j<ncolumns; j++){
89 latitude[j] = 0.0;
90 lontitude[j] = 0.0;
91 class[j]=0;
92 clear[j]=0;
93 tcumulus[j]=0;
94 cowacloud[j]=0;
95 cumulus[j]=0;
96 wawacloud[j]=0;
97 thickcicloud[j]=0;
98 verysmallcumu[j]=0;
99 cirrus[j]=0;
100 solidwa[j]=0;
101 cirrordeepcon[j]=0; 104 thwacloud1
[j]=0;
105 } 106 }
107 for(i=0; i<nrows; i++){
108 fread(latitude,sizeof(float),ncolumns,lat);


How were these files built so that binary data is in the correct
format for your system?
109
fread( lontitude,sizeof(float),ncolumns,lon);
110 fread(class,sizeof(short),ncolumns,in);
111 }
112 for(i=0;i<nrows;i++){
113 for(j=0;j<ncolumns;j++){
114 /* printf("The lat is %hd\t",class[j]);*/


I suspect you meant to print latitude[j] here. Don't forget to
change the format specification.
115 }
116 }
117
118 for(i=0; i<nrows; i++){
119 for(j=0; j<ncolumns; j++){

Please adopt a consistent indenting style. You make your code
difficult to read sometimes indenting and sometimes outdenting.
120 /*printf("the data is %hd",class[j]); */
121 if (class[j]==1){ clear[j]=1;}else {clear[j]=0;}
122 if (class[j]==2){ tcumulus[j]=1;}else {tcumulus
[j]=0;}
123 if (class[j]==3){ cowacloud[j]=1;}else {cowacloud
[j]=0;}
124 if (class[j]==4){ cumulus[j]=1;}else {cumulus[j]=0;}
125 if (class[j]==5){ wawacloud[j]=1;}else {wawacloud
[j]=0;}
126 if (class[j]==6){ thickcicloud[j]=1;}else
{thickcicloud[j]=0;}
127 if (class[j]==7){ verysmallcumu[j]=1;}else
{verysmallcumu[j]=0;}
128 if (class[j]==8){ cirrus[j]=1;}else {cirrus[j]=0;}
129 if (class[j]==9){ solidwa[j]=1;}else {solidwa[j]=0;}
130 if (class[j]==10){ thwacloud1[j]=1;}else {thwacloud1
[j]=0;}
131 if (class[j]==11){cirrordeepcon[j]=1;}else
{cirrordeepcon[j]=0;}
132 if (class[j]==12){ stratus[j]=1;}else {stratus
[j]=0;}
133 if (class[j]==13){ Cb[j]=1;}else {Cb[j]=0;}
134 /* printf("The lat is %hd\t",tcumulus[j]);*/
135 }
136 }
137
138 for(i=0; i<10; i++){
139 for(j=0; j<10; j++){printf("The lat is %d
\t",cumulus[j]);


This is not the latitude.
140 }}
141
142 WriteHeader(&psgrid, out);
143 :wq

I don't think vi commands compile well as c code.
144
145 Write2DArrayInt(tcumulus, ncolumns,nrows, out);
146 Write2DArrayInt(clear, ncolumns,nrows, out);
147 Write2DArrayInt(cowacloud, ncolumns,nrows, out);
148 Write2DArrayInt(cumulus, ncolumns,nrows, out);
149 Write2DArrayInt(wawacloud, ncolumns,nrows, out);
150 Write2DArrayInt(thickcicloud, ncolumns,nrows, out); 155
Write2DArrayInt(cirrordeepcon, ncolumns,nrows, out);
156 Write2DArrayInt(stratus, ncolumns,nrows, out);
157 Write2DArrayInt(Cb, ncolumns,nrows, out);
158
159 free_2D_Int(tcumulus);
160 free_2D_Int(clear);
161 free_2D_Int(cowacloud);
162 free_2D_Int(cumulus);
163 free_2D_Int(wawacloud);
164 free_2D_Int(thickcicloud);
165 free_2D_Int(verysmallcumu);
166 free_2D_Int(cirrus);
167 free_2D_Int(solidwa);
168 free_2D_Int(thwacloud1);
169 free_2D_Int(cirrordeepcon);
170 free_2D_Int(stratus);
171 free_2D_Int(Cb);
172
173
174 fclose(in);
175 fclose(out);
176 fclose(lat);
177 fclose(lon);
178
179 return(0);
180 }
181
182
183 void WriteHeader(GRIDDEF *psgrid, FILE *out)
184 {
185 fwrite(&(psgrid->CentLat),sizeof(double),1,out);
186 fwrite(&(psgrid->CentLon),sizeof(double),1,out);
187 fwrite(&(psgrid->DeltaX),sizeof(double),1,out);
188 fwrite(&(psgrid->DeltaY),sizeof(double),1,out);
189 fwrite(&(psgrid->ProjectionID),sizeof(long),1,out);
190 fwrite(psgrid->ProjectionParameters,sizeof(double),15,out);
191 fwrite(&(psgrid->MapZone),sizeof(long),1,out);
192 fwrite(&(psgrid->NumX),sizeof(int),1,out);
193 fwrite(&(psgrid->NumY),sizeof(int),1,out);
194 }
195
196
197 /* Function to create a 2D float array of pointers*/
198 short **Allocate2DInt(int rows,int columns) 206 return
pntr;

Why is line 206 duplicated here?
199 {
200 short **pntr;
201 int i;
202
203 pntr = (short **)malloc(sizeof(short *)*rows);

Don't cast the return from malloc.

You have a lot of faith that malloc will never fail.
204 pntr[0] = (short *)malloc(sizeof(short )*rows*columns);
205 for(i=1; i<rows; i++)pntr = pntr[i-1]+columns;
206 return pntr;
207 }
208
209 float **Allocate2DFloat(int rows,int columns)
210 {
211 float **pntr;
212 int i;
213
214 pntr = (float **)malloc(sizeof(float *)*rows);
215 pntr[0] = (float *)malloc(sizeof(float )*rows*columns);
216 for(i=1; i<rows; i++)pntr = pntr[i-1]+columns;
217 return pntr;
218 }
219
220 void Write2DArrayInt(short **Array, int Columns, int Rows,
FILE *fp)
221 {
222
223 int i;
224
225 for(i=0; i<Rows; i++){
226 fwrite(Array, sizeof(short),Columns, fp);
227 }
228 /*fclose(fp);*/
229 }
230
231 void free_2D_float(float **pntr)
232 {
233
234 free(pntr[0]);
235
236 free(pntr);
237 }
238
239 void free_2D_Int(short **pntr)
240 {
241
242 free(pntr[0]);
243
244 free(pntr);
245 }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top