openning a file

X

xiao

Does anyone tried to open a file with extension of .lat using C? why
I cannot open these files using fopen? Any other way that I can try
to open it? Thank you~
 
X

xiao

I just write like this:

FILE *lat;
FILE *lon;
long lati[2030][1354],loni[2030][1354];

lat=fopen("MOD021KM.A2005243.0255.005.2008027115345.lat","r");
lon=fopen("MOD021KM.A2005243.0255.005.2008027115345.lon","r");

And it reminds me Segmentation fault.... :(
 
W

Willem

xiao wrote:
) I just write like this:
)
) FILE *lat;
) FILE *lon;
) long lati[2030][1354],loni[2030][1354];

Remove this line -----^
and try again. (Just a hunch.)

) lat=fopen("MOD021KM.A2005243.0255.005.2008027115345.lat","r");
) lon=fopen("MOD021KM.A2005243.0255.005.2008027115345.lon","r");
)
) And it reminds me Segmentation fault.... :(

How do you know the segfault is on the fopen() call ?


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
X

xiao

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?
 
F

Fred

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?

What does not work? how does it not work?
What file do you mean with is "in the file"?
What number are you talking about that is a 1D array?
Define what as a 2D array?
 
W

Willem

xiao wrote:
) 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?

The code you showed only opens the file, it does not read the file.
So the fault can not happen in the code you showed.

Please show the rest of the code, that actually reads the file.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
J

Johannes Bauer

Willem said:
The code you showed only opens the file, it does not read the file.
So the fault can not happen in the code you showed.

Yes, it can. On my machine this program:

#include <stdio.h>
int main() {
unsigned char foo[N * 1024 * 1024];
foo[0] = 0;
printf("%d\n", foo[0]);
return 0;
}

Works when compiled with -DN=7, but segfaults when compiled with -DN=8.
It obviously crashes by a seg fault which may well be the case with the
OPs program.

Regards,
Johannes
 
X

xiao

Sorry, here is the code:


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

int main()
{
FILE *lat;
FILE *lon;


lat=fopen("MOD021KM.A2005243.0255.005.2008027115345.lat","r");
lon=fopen("MOD021KM.A2005243.0255.005.2008027115345.lon","r");


long lati[2030][1354],loni[2030][1354];

fread(lati,sizeof(lati),1,lat);
fread(loni,sizeof(loni),1,lon);

printf("The 00 is %ld",lati[0][0]);

fclose(lat);
fclose(lon);
return (0);
}
 
F

Flash Gordon

xiao wrote, On 05/08/08 18:58:
I just write like this:

FILE *lat;
FILE *lon;
long lati[2030][1354],loni[2030][1354];

lat=fopen("MOD021KM.A2005243.0255.005.2008027115345.lat","r");
lon=fopen("MOD021KM.A2005243.0255.005.2008027115345.lon","r");

And it reminds me Segmentation fault.... :(

Something is wrong in one of the many lines of code you have not shown.

Please post a small complete compilable example that shows the problem.
 
S

santosh

xiao said:
Sorry, here is the code:


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

int main()
{
FILE *lat;
FILE *lon;


lat=fopen("MOD021KM.A2005243.0255.005.2008027115345.lat","r");
lon=fopen("MOD021KM.A2005243.0255.005.2008027115345.lon","r");

Check that these calls actually do manage to open their files.
long lati[2030][1354],loni[2030][1354];

On a system where a long is four bytes these declarations will consume
about 22 megabytes of automatic memory. On a system with eight byte
longs it will be about 43 megabytes. Many systems are unable to
allocate automatic objects of such size. You might either want to
reduce their sizes or allocate them dynamically with *alloc.
fread(lati,sizeof(lati),1,lat);
fread(loni,sizeof(loni),1,lon);

These calls are rather fragile. You might try reading 2030 objects of
long [1354] or 2748620 objects of long rather than one huge object.

In any case check that the calls have actually completed successfully.
printf("The 00 is %ld",lati[0][0]);

fclose(lat);
fclose(lon);
return (0);
}
 
S

santosh

Dann said:
news:9c92b95d-30fe-445a-9be6-97bd63033029@e39g2000hsf.googlegroups.com...
Sorry, here is the code:


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

int main()
{
FILE *lat;
FILE *lon;


lat=fopen("MOD021KM.A2005243.0255.005.2008027115345.lat","r");
lon=fopen("MOD021KM.A2005243.0255.005.2008027115345.lon","r");


long lati[2030][1354],loni[2030][1354];

fread(lati,sizeof(lati),1,lat);
fread(loni,sizeof(loni),1,lon);

printf("The 00 is %ld",lati[0][0]);

fclose(lat);
fclose(lon);
return (0);
}

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

static long lati[2030][1354],
loni[2030][1354];

int main()
{
FILE *lat;
FILE *lon;

lat = fopen("MOD021KM.A2005243.0255.005.2008027115345.lat", "r");
lon = fopen("MOD021KM.A2005243.0255.005.2008027115345.lon", "r");

if (lat != NULL && lon != NULL) {
size_t lat_read,
lon_read;
lat_read = fread(lati, sizeof(lati), 1, lat);
lon_read = fread(loni, sizeof(loni), 1, lon);
if (lat_read != sizeof lati) {

Fread returns the number of objects it successfully read and stored, not
their total size. If the above fread calls fail, the return value will
be zero, even if only the last byte could not be read.

<snip>
 
F

Flash Gordon

xiao wrote, On 05/08/08 19:56:
Sorry, here is the code:

Please provide enough context for your post to stand on its own.
Sometimes posts arrive out of order, sometimes posts don't arrive at all.

Your problem was a seg fault which you could thought was in fopen.

long lati[2030][1354],loni[2030][1354];

Others have already suggested that the above arrays are almost certainly
too large. They are correct (at least on this machine). The place where
gdb indicates a seg fault is not always the line of C where the problem is.

You might be able so allocate these at file scope (an ugly solution that
happens to work on my machine) or allocate the space using malloc (see
the comp.lang.c FAQ for how to allocate 2D arrays using malloc).
 
C

CBFalconer

xiao said:
Sorry, here is the code:

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

int main() {
FILE *lat;
FILE *lon;

lat=fopen("MOD021KM.A2005243.0255.005.2008027115345.lat","r");
lon=fopen("MOD021KM.A2005243.0255.005.2008027115345.lon","r");

long lati[2030][1354],loni[2030][1354];

This line belongs above, with the FILE* definitions.
fread(lati,sizeof(lati),1,lat);
fread(loni,sizeof(loni),1,lon);

printf("The 00 is %ld",lati[0][0]);

fclose(lat);
fclose(lon);
return (0);
}

I have corrected your indentation.
 
B

Bartc

xiao said:
Sorry, here is the code:


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

int main()
{
FILE *lat;
FILE *lon;


lat=fopen("MOD021KM.A2005243.0255.005.2008027115345.lat","r");
lon=fopen("MOD021KM.A2005243.0255.005.2008027115345.lon","r");

What are the values of lat and lon at this point?
 
K

Keith Thompson

xiao said:
hum....,nothing read in here...

When you post a followup, please quote enough context from the parent
article so that your followup makes sense to someone who hasn't seen
the parent article.

Google Groups already does this for you. Just delete any quoted
material that isn't relevant to what you're saying and leave the rest
in place (along with the attribution line, the one that says
"So-and-so writes:").
 
K

Keith Thompson

Eric Sosman said:
xiao said:
I just write like this:
FILE *lat;
FILE *lon;
long lati[2030][1354],loni[2030][1354];
lat=fopen("MOD021KM.A2005243.0255.005.2008027115345.lat","r");
lon=fopen("MOD021KM.A2005243.0255.005.2008027115345.lon","r");
And it reminds me Segmentation fault.... :(

You've still not quite mastered the art of making a good
problem report, but this *may* be Question 16.3 in the comp.lang.c
Frequently Asked Questions (FAQ) list, <http://www.c-faq.com/>.
Note that the two arrays use about 21 MB, perhaps more.

That's likely.

I find it amusing that the answer says:

You probably have one or more very large (kilobyte or more) local
arrays.

Is the FAQ really *that* old?
 
X

xiao

When you post a followup, please quote enough context from the parent
article so that your followup makes sense to someone who hasn't seen
the parent article.

Google Groups already does this for you. Just delete any quoted
material that isn't relevant to what you're saying and leave the rest
in place (along with the attribution line, the one that says

"So-and-so writes:").

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Thank u....And I do think it is because the array is too big and there
is the code written by Dann and the result is
ERROR: Not all lat data read. You can try to solve it
here.
P.S.\nTrying to read 2,748,620 * sizeof long bites in one
gulp is iffy


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

static long lati[2030][1354],
loni[2030][1354];

int main()
{
FILE *lat;
FILE *lon;

lat = fopen("MOD021KM.A2005243.0255.005.2008027115345.lat", "r");
lon = fopen("MOD021KM.A2005243.0255.005.2008027115345.lon", "r");

if (lat != NULL && lon != NULL) {
size_t lat_read,
lon_read;
lat_read = fread(lati, sizeof(lati), 1, lat);
lon_read = fread(loni, sizeof(loni), 1, lon);
if (lat_read != sizeof lati) {
puts("ERROR: Not all lat data read. You can try to solve
it here.\t");
puts("P.S.\nTrying to read 2,748,620 * sizeof long bites
in one gulp is iffy.\t");
exit(EXIT_FAILURE);
}
if (lon_read != sizeof loni) {
puts("ERROR: Not all lon data read. You can try to solve
it here.\t");
puts("P.S.\nTrying to read 2,748,620 * sizeof long bites
in one gulp is iffy.\t");
exit(EXIT_FAILURE);
}
printf("The 00 is %ld", lati[0][0]);

fclose(lat);
fclose(lon);
} else {
perror("ERROR: Unable to open one or more input file");
exit(EXIT_FAILURE);
}
return 0;

}
 
K

Keith Thompson

CBFalconer said:
xiao said:
Sorry, here is the code:

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

int main() {
FILE *lat;
FILE *lon;

lat=fopen("MOD021KM.A2005243.0255.005.2008027115345.lat","r");
lon=fopen("MOD021KM.A2005243.0255.005.2008027115345.lon","r");

long lati[2030][1354],loni[2030][1354];

This line belongs above, with the FILE* definitions.
fread(lati,sizeof(lati),1,lat);
fread(loni,sizeof(loni),1,lon);

printf("The 00 is %ld",lati[0][0]);

fclose(lat);
fclose(lon);
return (0);
}

I have corrected your indentation.

Really? You like to put the opening brace on the same line with "int
main()" and the closing brace indented at the same level as the return
statement? Nothing wrong with that, but it's not a style I've seen.

What is your basis for assuming that the OP doesn't have a C99
compiler?

At the very least, it would be helpful to explain *why* the
declaration belongs before the assignment statements.

xiao: C99 allows declarations and statements to be mixed within a
block. C90 requires all statements within a block to follow all
declarations. Most C compilers still don't fully support C99, though
many of them support parts of it; in particular, allowing mixed
declarations and statements is a very common extension. If you want
your code to be maximally portable, you shouldn't take advantage of
this feature.
 

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top