openning a file

X

xiao

Keith Thompson said:



Presumably the same as his basis for assuming that the OP doesn't ride a
unicorn. :)

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Ok guys , i decided to write it again like this:

34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 float **Allocate2DFloat(int rows, int columns)
38
39 int main()
40 {
41 FILE *lat,*lon;
42 int i,j;
43 float **latitude;
44
45 lat =
fopen("MOD021KM.A2005243.0255.005.2008027115345.lat", "r");
46 lon =
fopen("MOD021KM.A2005243.0255.005.2008027115345.lon", "r");
47
48 latitude=Allocate2DFloat(nrows, ncolumns);
49
50
51 for(i=0; i<nrows; i++){
52 for(j=0; j<ncolumns; j++){
53
54 latitude[j] = 0.0;
55
56 }
57 }
58
59
60 for(i=0; i<nrows; i++){
61 fread(latitude,sizeof(float),ncolumns,lat);
62 }
63 close(lat);
64 return(0);
65 }
66 /* Function to create a 2D float array of pointers*/
67
68 float **Allocate2DFloat(int rows, int columns)
69 {
70 float **pntr;
71
72
73 pntr = (float **)malloc(sizeof(float*)*rows);
74 pntr[0] = (float *)malloc(sizeof(float)*rows*columns);
75 for(i=1; i<rows; i++)pntr = pntr[i-1]+columns;
76 return pntr;
77 }

But it saids:

ReadClassFiletest.c: In function `Allocate2DFloat':
ReadClassFiletest.c:40: warning: 'main' is usually a function
ReadClassFiletest.c:40: error: syntax error before '{' token
ReadClassFiletest.c:45: error: syntax error before "lat"


Why is that? :(
 
J

Jens Thoms Toerring

xiao said:
Ok guys , i decided to write it again like this:
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 float **Allocate2DFloat(int rows, int columns)

You forgot the semicolon at the end of this line.
38
39 int main()
40 {
41 FILE *lat,*lon;
42 int i,j;
43 float **latitude;
44
45 lat =
fopen("MOD021KM.A2005243.0255.005.2008027115345.lat", "r");
But it saids:
ReadClassFiletest.c: In function `Allocate2DFloat':
ReadClassFiletest.c:40: warning: 'main' is usually a function
ReadClassFiletest.c:40: error: syntax error before '{' token
ReadClassFiletest.c:45: error: syntax error before "lat"

I can only guess about this last line that the compiler got
thrown off track by the first error. What happens if you add
the semicolon?
Regards, Jens
 
B

Ben Bacarisse

It is best not to quote sig blocks (unless you are talking about them).
Ok guys , i decided to write it again like this:

34 #include <stdlib.h>

What's in the other 33 lines?
35 #include <stdio.h>
36 #include <string.h>
37 float **Allocate2DFloat(int rows, int columns)

Missing semicolon ';' at the end.
38
39 int main()
40 {
41 FILE *lat,*lon;
42 int i,j;
43 float **latitude;
44
45 lat =
fopen("MOD021KM.A2005243.0255.005.2008027115345.lat", "r");
46 lon =
fopen("MOD021KM.A2005243.0255.005.2008027115345.lon", "r");
47
48 latitude=Allocate2DFloat(nrows, ncolumns);

Are these two variable defined in the missing 33 lines?

If you don't start testing the result from things that can fail,
people will lose patience. At the very least put all the work inside
an if (lat && lon && latitude) { ... }. You know, there are languages
that throw exceptions, and you may prefer one of them -- C need lots
of tests that things worked.
49
50
51 for(i=0; i<nrows; i++){
52 for(j=0; j<ncolumns; j++){
53
54 latitude[j] = 0.0;
55
56 }
57 }


Aren't you going to read values into this array?
58
59
60 for(i=0; i<nrows; i++){
61 fread(latitude,sizeof(float),ncolumns,lat);
62 }
63 close(lat);
64 return(0);
65 }
66 /* Function to create a 2D float array of pointers*/
67
68 float **Allocate2DFloat(int rows, int columns)
69 {
70 float **pntr;
71
72
73 pntr = (float **)malloc(sizeof(float*)*rows);
74 pntr[0] = (float *)malloc(sizeof(float)*rows*columns);


You don't need the casts, but you do need to check the result.
75 for(i=1; i<rows; i++)pntr = pntr[i-1]+columns;
76 return pntr;
77 }

But it saids:

ReadClassFiletest.c: In function `Allocate2DFloat':
ReadClassFiletest.c:40: warning: 'main' is usually a function
ReadClassFiletest.c:40: error: syntax error before '{' token
ReadClassFiletest.c:45: error: syntax error before "lat"


Why is that? :(


The missing ;.
 
X

xiao

     It's in the garden, cropping the roses.

Thank you very much~~~it works, but I still got a question. How can i
print out the value that read out ?
Put a printf like this? printf("The data is %f\t",&latitude) ?
:) Thanks
 
C

CBFalconer

xiao said:
Thank you very much~~~it works, but I still got a question. How
can i print out the value that read out ?
Put a printf like this? printf("The data is %f\t",&latitude) ?

I fail to see how that will print any unicorns. They are extremely
fussy beasts, and can easily impale non-believers.
 
S

santosh

xiao said:
Thank you very much~~~it works, but I still got a question. How can i
print out the value that read out ?
Put a printf like this? printf("The data is %f\t",&latitude) ?
:) Thanks

The %f conversion specifier expects an argument of type double. A float
argument is also automatically converted to the correct type, but a
pointer argument is wrong. You may want to remove that address-of
operator.
 
N

Nick Keighley

don't remove the context
humm....it does not work, I use DDD to do the debugging. I was
thinking, if the number in the file is a 1D array but I define it as a
2D array , will this fault happen?

post your code.

That is, post a short complete, compilable example and explain what
your
problem is.
 
X

xiao

(because (wants he ((to write) lisp))

That is the code, Thank you~


for(i=0; i<nrows; i++){
fread(latitude,sizeof(float),ncolumns,lat);
printf("The lat is %f",latitude);
}
fclose(lat);
 
S

santosh

That is the code, Thank you~


for(i=0; i<nrows; i++){
fread(latitude,sizeof(float),ncolumns,lat);


So, the type of latitude must be float* or void* derived from a
float*. Check that fread does return ncolumns, i.e., the I/O has
successfully been completed.
printf("The lat is %f",latitude);

This is wrong. The 'f' type specifier expects a double argument. A float
argument is also compatible. But what you are actually passing it
(latitude) appears (from this fragment at least) to be either float**
or float (*)[]. You probably want latitude[j] or similar.

<snip>

PS. When you make corrections/updates to your program and want to post
ask again about it, please repost the whole, compilable, source for
your program and not fragments like the one above. We need to see the
declarations and try to (if possible) compile it ourselves to get a
good idea of what may be wrong. Unless you are very sure about the
place where an error occurs, don't omit portions of the program, even
though you may think that they have no bearing on the problem
specified. Of course, this rule doesn't apply when your program grows
beyond about 200 lines or so, but until then, try to post complete (and
if possible, compilable) code.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top