openning a file

Discussion in 'C Programming' started by xiao, Aug 5, 2008.

  1. xiao

    xiao Guest

    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~
    xiao, Aug 5, 2008
    #1
    1. Advertising

  2. xiao

    xiao Guest

    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.... :(
    xiao, Aug 5, 2008
    #2
    1. Advertising

  3. xiao

    Willem Guest

    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
    Willem, Aug 5, 2008
    #3
  4. xiao

    xiao Guest

    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?
    xiao, Aug 5, 2008
    #4
  5. xiao

    Fred Guest

    On Aug 5, 11:14 am, 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?


    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?
    --
    Fred
    Fred, Aug 5, 2008
    #5
  6. xiao

    Willem Guest

    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
    Willem, Aug 5, 2008
    #6
  7. Willem schrieb:

    > 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

    --
    "Wer etwas kritisiert muss es noch lange nicht selber besser können. Es
    reicht zu wissen, daß andere es besser können und andere es auch
    besser machen um einen Vergleich zu bringen." - Wolfgang Gerber
    in de.sci.electronics <47fa8447$0$11545$>
    Johannes Bauer, Aug 5, 2008
    #7
  8. xiao

    xiao Guest

    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);
    }
    xiao, Aug 5, 2008
    #8
  9. xiao

    Flash Gordon Guest

    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.
    --
    Flash Gordon
    Flash Gordon, Aug 5, 2008
    #9
  10. xiao

    santosh Guest

    xiao wrote:

    > 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);
    > }
    santosh, Aug 5, 2008
    #10
  11. xiao

    santosh Guest

    Dann Corbit wrote:

    > "xiao" <> wrote in message
    >

    news:...
    >> 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>
    santosh, Aug 5, 2008
    #11
  12. xiao

    Flash Gordon Guest

    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.

    <snip>

    > 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).
    --
    Flash Gordon
    Flash Gordon, Aug 5, 2008
    #12
  13. xiao

    xiao Guest

    hum....,nothing read in here...
    xiao, Aug 5, 2008
    #13
  14. xiao

    Default User Guest

    xiao wrote:

    > hum....,nothing read in here...


    What?




    Brian
    Default User, Aug 5, 2008
    #14
  15. xiao

    CBFalconer Guest

    xiao wrote:
    >
    > 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.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Aug 5, 2008
    #15
  16. xiao

    Bartc Guest

    "xiao" <> wrote in message
    news:...
    > 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?

    --
    Bartc
    Bartc, Aug 5, 2008
    #16
  17. xiao <> writes:
    > 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:").

    --
    Keith Thompson (The_Other_Keith) <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"
    Keith Thompson, Aug 5, 2008
    #17
  18. Eric Sosman <> writes:
    > xiao wrote:
    >> 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?

    --
    Keith Thompson (The_Other_Keith) <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"
    Keith Thompson, Aug 5, 2008
    #18
  19. xiao

    xiao Guest

    On Aug 5, 5:06 pm, Keith Thompson <> wrote:
    > xiao <> writes:
    > > 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:").
    >
    > --
    > Keith Thompson (The_Other_Keith) <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;

    }
    xiao, Aug 5, 2008
    #19
  20. CBFalconer <> writes:
    > xiao wrote:
    >> 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.

    --
    Keith Thompson (The_Other_Keith) <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"
    Keith Thompson, Aug 6, 2008
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mel
    Replies:
    5
    Views:
    495
    Jan Harders
    Jul 18, 2004
  2. DJ
    Replies:
    6
    Views:
    325
    John Harrison
    Oct 27, 2004
  3. Replies:
    1
    Views:
    279
    Alf P. Steinbach
    Apr 17, 2006
  4. Replies:
    2
    Views:
    266
    Alf P. Steinbach
    Apr 17, 2006
  5. ER
    Replies:
    2
    Views:
    492
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Aug 12, 2007
Loading...

Share This Page