jam1
Posts: 47
Joined: Tue Oct 21, 2014 5:51 pm

Reading a file larger than 4096 bytes in C

Fri May 08, 2015 11:31 pm

Hello world,

I am having a strange problem today. I was running a program to read a file in C and it was working fine until I had to open a larger file. (not much larger but above 4096 bytes, it stops working). This is the limit of 12 bits (size) but if I check the sizeof(char), sizeof(short) or sizeof(int), I get 1, 2, 4 bytes words length respectively. I am using fseek, ftell to get the file size.
Any enlightenment would be welcome.

This is the program : reading_ascii.c

Code: Select all

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

int main(){

  FILE *in;
   int nblocks_start,nblocks_end;

        in = fopen("demo0004.txt", "r,ccs=US-ASCII");
        if (in == NULL) {
                perror("demo0004.txt");
                exit(1);
        }

    if(fseek(in, 0L, SEEK_END) != 0) {
    perror("fseek(in, 0L, SEEK_END");
    exit(1);
    }
        nblocks_end = ftell(in);
//      rewind(in);
    if(fseek(in, 0L, SEEK_SET) !=0) {
    perror("fseek(in, 0L, SEEK_END");
    exit(1);
    }
        nblocks_start = ftell(in);
        printf ( " %d  %d  \n",nblocks_start,nblocks_end);

        int i;
    while ( i<24){
        printf (" 0x%xl \n",getc(in));
        i++;
        }

}

And its data file : demo0004.txt

Code: Select all

1211211211211211211211211211211211211211211211211211211211211211
2112112112112112112112112112112112112112112112112112112112112112
1121121121121121121121121121121121121121121121121121121121121121
1211211211211211211211211211211211211211211211211211211211211211
2112112112112112112112112112112112112112112112112112112112112112
1121121121121121121121121121121121121121121121121121121121121121
1211211211211211211211211211211211211211211211211211211211211211
2112112112112112112112112112112112112112112112112112112112112112
1121121121121121121121121121121121121121121121121121121121121121
121121121121121121121121
2222222222222222222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222222222222222222
2222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000
6666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666
6666666666666666666666666666666666666666666666666666666666666666
66666666666666666666666666666666
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
4444444444444444444444444444444444444444444444444444444444444444
444444444444444444444444444444444444444444444444
I compille with
gcc -o reading_ascii reading_ascii.c

and run with
./reading_ascii

If you trim the data file to be shorter than 4096 bytes, it will print the proper value on output.
Thanks.

User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Reading a file larger than 4096 bytes in C

Sat May 09, 2015 12:57 am

In what way is it not printing the proper result. Note that you may not be getting what you expect at the end as you declare i but never initialise it to anything so the code in the while loop might not get executed.
jam1 wrote:

Code: Select all

    int i;   // You never initialise i so it may have a value >= 24
    while ( i<24){
        printf (" 0x%xl \n",getc(in));
        i++;
    }
Also, for me, having the "ccs=US-ASCII" in the fopen causes getc() to always return EOF (and hence 0xffffffffl).
Last edited by Paeryn on Sat May 09, 2015 1:33 am, edited 1 time in total.
She who travels light — forgot something.

User avatar
jojopi
Posts: 3271
Joined: Tue Oct 11, 2011 8:38 pm

Re: Reading a file larger than 4096 bytes in C

Sat May 09, 2015 1:09 am

Do you need the ccs=US-ASCII? I guess not, because ASCII is not wide and you are not using fgetwc() anyway.

Any kind of character conversion is also likely to be incompatible with seeking. You can get the size in bytes with fstat() on fileno(), but for the size in characters you must read until EOF/WEOF.

jam1
Posts: 47
Joined: Tue Oct 21, 2014 5:51 pm

Re: Reading a file larger than 4096 bytes in C

Sat May 09, 2015 2:16 am

jojopi wrote:Do you need the ccs=US-ASCII? I guess not, because ASCII is not wide and you are not using fgetwc() anyway.

Any kind of character conversion is also likely to be incompatible with seeking. You can get the size in bytes with fstat() on fileno(), but for the size in characters you must read until EOF/WEOF.
Thank you, I remove ccs=US-ASCII, which I was using for another purpose and now it works for any size. It still does not explain the 12 bit size limit but it will be for another day.

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Reading a file larger than 4096 bytes in C

Sat May 09, 2015 5:37 am

Did you try what Paeryn said about setting the initial value of i?

You could either do something like

Code: Select all

    int i;
    while ( i<24){
        printf (" 0x%xl \n",getc(in));
        i++;
    }
or change it to a for loop

Code: Select all

    for(i=0;i<24;i++){
        printf (" 0x%xl \n",getc(in));
    }

Return to “C/C++”