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++;
}
}
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
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.