Cannot Open FILE-using fopen()

This is a discussion on Cannot Open FILE-using fopen() within the C Programming forums, part of the General Programming Boards category; Hi All, I am trying to open a big file of more than 8GB using the fopen().But its giving error.If ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    22

    Lightbulb Cannot Open FILE-using fopen()

    Hi All,
    I am trying to open a big file of more than 8GB using the fopen().But its giving error.If i will take small file i mean less than of 2GB ascii file its working fine.

    I just want to know is it any file size limitation for FILE data type.

    Even same thing i am getting error with fgets() also.

    How we will read the Huge ascii file ?

    Any idea or thoughts would be appreciable.


    Thanks in advance

    Anwar

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    Have a look at the read() function.

    EDIT: Oh yeah, I suspect an int would be used as a file position marker. Unsigned can only point at a 4Gig range.

    I doubt whether there would be any limits within the language itself though. Your compiler will be doing it.
    Last edited by NickESP; 02-07-2006 at 11:44 PM.
    "It compiled, let's ship it!"

    Guitar Australia
    my site for some easy tutorials.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    22

    Lightbulb

    Hi ,
    Thnkx for response.But really i didnt get what u r trying 2 say.
    i m sending my code.Can you look and let me know where exactly require the change..

    Code:
    #include        <stdio.h>
    
    int getrec(FILE *);
    
    main(int argc, char *argv[])
    {
            int     n;
            unsigned     recno = 0;      /* number of records */
            int     minrec;   /* smallest */
            int     maxrec;       /* largest */
            long    cumrec = 0;     /* cumulative size */
            FILE    *dfp;           /* file to analyse */
      char  started = 0;
            if(argc != 2)
            {
                    printf("Usage : fil2 f\n");
                    exit(1);
            }
            if( (dfp = fopen(argv[1],"r")) == NULL)
            {
                    printf("error opening %s\n",
              argv[1]);
                    exit(1);
            }
            while (1)
            {
                    if((n=getrec(dfp))==EOF) break;
                    recno++;
                    cumrec += n;
        if(!started)
        {
          minrec = maxrec = n;
          started = 1;
        }
                    if(n<minrec) minrec = n;
                    if(n>maxrec) maxrec = n;
            }
            printf("%4d records\n",recno);
            printf("average size %5.1f\n", (double)cumrec/recno);
            printf("smallest %2d\nlargest %4d\n",
            minrec,maxrec);
    }
    int getrec(FILE *f)
    
    {
            int     rs=0;
            int     length;
            char    buff[25];
            while(1)
            {
                    if(fgets(buff,25,f)==NULL)
                            return EOF;
                    length = strlen(buff);
                    rs += length;
                    if(buff[length - 1] == '\n')
                            return rs;
            }
    }
    Regards,
    Anwar

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    The logic looks fine. Have you found out what the error number is?

    Code:
    #include<errno.h>
    printf("%d was the error number\n", EDOM);
    This might tell you why it failed.
    "It compiled, let's ship it!"

    Guitar Australia
    my site for some easy tutorials.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    You might have to look up whatever "large file" API your OS provides, because it looks like your compiler/libraries limit the upper size of files.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    Hi,
    The error number was 33.Can you tel me what could be the reason.


    Thanks.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    But actual the errorno is showing 72 i mean

    printf(" %d was the error number\n", errno);

    so its printing 72.

    psl let me know do you have any idea,wht could be the reason.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Use perror() to get a word description.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    Hi salem,
    i have done any now getting following error

    72 was the error number
    Value too large to be stored in data type
    Error: Cannot get file properties

    that error only comes when my file size is more than 2 GB.

    can u tel me is it any FILE data type size limitation.
    u can refer program.


    Anwar

  10. #10
    old man
    Join Date
    Dec 2005
    Posts
    90
    If you're using linux, google on LFS (large file support). You just need to compile with an extra flag or two with gcc in order to change off_t from 32- to 64-bit.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    22
    Hi
    Thanks for helping.u r right now my program is working fine for bigger file.

    while compiling i have added extra parameter
    -D _FILE_OFFSET_BITS=64

    thanks once again.


    Anwar

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21