openning a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xiao

    openning a file

    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~
  • Eric Sosman

    #2
    Re: openning a file

    xiao wrote:
    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~
    There's no obvious difficulty. What code did you use, and what
    exactly do you mean by "cannot open it?" Did the fopen() call fail,
    if so what if anything did perror() show, if not what do you mean?

    --
    Eric.Sosman@sun .com

    Comment

    • xiao

      #3
      Re: openning a file

      I just write like this:

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

      lat=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.lat","r" );
      lon=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.lon","r" );

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

      Comment

      • Willem

        #4
        Re: openning a file

        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("MOD0 21KM.A2005243.0 255.005.2008027 115345.lat","r" );
        ) lon=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.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

        Comment

        • xiao

          #5
          Re: openning a file

          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?

          Comment

          • Willem

            #6
            Re: openning a file

            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

            Comment

            • Fred

              #7
              Re: openning a file

              On Aug 5, 11:14 am, xiao <littledd...@gm ail.comwrote:
              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

              Comment

              • Eric Sosman

                #8
                Re: openning a file

                xiao wrote:
                I just write like this:
                >
                FILE *lat;
                FILE *lon;
                long lati[2030][1354],loni[2030][1354];
                >
                lat=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.lat","r" );
                lon=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.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.

                --
                Eric.Sosman@sun .com

                Comment

                • Johannes Bauer

                  #9
                  Re: openning a file

                  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.electron ics <47fa8447$0$115 45$9b622d9e@new s.freenet.de>

                  Comment

                  • xiao

                    #10
                    Re: openning a file

                    Sorry, here is the code:


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

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


                    lat=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.lat","r" );
                    lon=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.lon","r" );


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

                    fread(lati,size of(lati),1,lat) ;
                    fread(loni,size of(loni),1,lon) ;

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

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

                    Comment

                    • Dann Corbit

                      #11
                      Re: openning a file

                      "xiao" <littledddna@gm ail.comwrote in message
                      news:9c92b95d-30fe-445a-9be6-97bd63033029@e3 9g2000hsf.googl egroups.com...
                      Sorry, here is the code:
                      >
                      >
                      #include <stdlib.h>
                      #include <stdio.h>
                      #include <string.h>
                      >
                      int main()
                      {
                      FILE *lat;
                      FILE *lon;
                      >
                      >
                      lat=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.lat","r" );
                      lon=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.lon","r" );
                      >
                      >
                      long lati[2030][1354],loni[2030][1354];
                      >
                      fread(lati,size of(lati),1,lat) ;
                      fread(loni,size of(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.20080271153 45.lat", "r");
                      lon = fopen("MOD021KM .A2005243.0255. 005.20080271153 45.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.");
                      puts("P.S.\nTry ing to read 2,748,620 * sizeof long bites in one
                      gulp is iffy.");
                      exit(EXIT_FAILU RE);
                      }
                      if (lon_read != sizeof loni) {
                      puts("ERROR: Not all lon data read. You can try to solve it
                      here.");
                      puts("P.S.\nTry ing to read 2,748,620 * sizeof long bites in one
                      gulp is iffy.");
                      exit(EXIT_FAILU RE);
                      }
                      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_FAILU RE);
                      }
                      return 0;
                      }

                      ** Posted from http://www.teranews.com **

                      Comment

                      • santosh

                        #12
                        Re: openning a file

                        xiao wrote:
                        Sorry, here is the code:
                        >
                        >
                        #include <stdlib.h>
                        #include <stdio.h>
                        #include <string.h>
                        >
                        int main()
                        {
                        FILE *lat;
                        FILE *lon;
                        >
                        >
                        lat=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.lat","r" );
                        lon=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.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,size of(lati),1,lat) ;
                        fread(loni,size of(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);
                        }

                        Comment

                        • santosh

                          #13
                          Re: openning a file

                          Dann Corbit wrote:
                          "xiao" <littledddna@gm ail.comwrote in message
                          >
                          news:9c92b95d-30fe-445a-9be6-97bd63033029@e3 9g2000hsf.googl egroups.com...
                          >Sorry, here is the code:
                          >>
                          >>
                          > #include <stdlib.h>
                          > #include <stdio.h>
                          > #include <string.h>
                          >>
                          > int main()
                          > {
                          > FILE *lat;
                          > FILE *lon;
                          >>
                          >>
                          > lat=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.lat","r" );
                          > lon=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.lon","r" );
                          >>
                          >>
                          > long lati[2030][1354],loni[2030][1354];
                          >>
                          > fread(lati,size of(lati),1,lat) ;
                          > fread(loni,size of(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.20080271153 45.lat", "r");
                          lon = fopen("MOD021KM .A2005243.0255. 005.20080271153 45.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>

                          Comment

                          • xiao

                            #14
                            Re: openning a file

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

                            Comment

                            • Flash Gordon

                              #15
                              Re: openning a file

                              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("MOD0 21KM.A2005243.0 255.005.2008027 115345.lat","r" );
                              lon=fopen("MOD0 21KM.A2005243.0 255.005.2008027 115345.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

                              Comment

                              Working...