Not able to read large files

This is a discussion on Not able to read large files within the C Programming forums, part of the General Programming Boards category; HI I am trying to go through large file handling using C. I have a text file called test size ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    24

    Not able to read large files

    HI I am trying to go through large file handling using C. I have a text file called test size is more than 6 GB. Following is my code :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    
    #define _FILE_OFFSET_BITS 64
    
    int main(void)
    {
    	FILE *fptr;
    	off64_t ftell_output;
    	int count = 0;
    	off64_t fseek_offset = 2147483634;	
    	
    	fptr = fopen64("/PARIMAL/test","r");
    	if(fptr == NULL)
    	{
    		perror("fptr is null");
    	}
    	
    	while (count == 0)
    	{
            	if ((ftell_output = ftello64(fptr)) < 0)
    		{
    			perror("ftell failed");
    		}
    		printf("value of ftell_output is %lld\n",ftell_output);
    		if(fseeko64(fptr ,fseek_offset++,SEEK_SET) < 0)
    		{	
    			perror("error feesk");
    			exit(2);
    		}
    	}
    
    	return 0;
    }
    the output is :
    value of ftell_output is 2147483634
    value of ftell_output is 2147483635
    value of ftell_output is 2147483636
    value of ftell_output is 2147483637
    value of ftell_output is 2147483638
    value of ftell_output is 2147483639
    value of ftell_output is 2147483640
    value of ftell_output is 2147483641
    value of ftell_output is 2147483642
    value of ftell_output is 2147483643
    value of ftell_output is 2147483644
    value of ftell_output is 2147483645
    value of ftell_output is 2147483646
    value of ftell_output is 2147483647
    ftell failed: Error 0
    value of ftell_output is -2147483648
    ftell failed: Error 0
    value of ftell_output is -2147483647
    ftell failed: Error 0
    value of ftell_output is -2147483646
    ftell failed: Error 0
    value of ftell_output is -2147483645
    ftell failed: Error 0
    value of ftell_output is -2147483644
    ftell failed: Error 0
    value of ftell_output is -2147483643
    ftell failed: Error 0
    value of ftell_output is -2147483642
    ftell failed: Error 0
    value of ftell_output is -2147483641
    ftell failed: Error 0
    value of ftell_output is -2147483640
    ftell failed: Error 0

    Why does the return of ftell go negative ??

    for compilation am using following command :
    cc -o program -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE file.c

    Please help.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You're getting integer overflow. 2147483648 is too big for an off64_t to hold.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    What system do you use (OS, 32 or 64 bit)?

    When I remove all 64's in your code (off_t instead of off64_t, etc.) and compile just with -D_FILE_OFFSET_BITS=64 (no other flags) your code runs fine on my machine (Lubuntu 11.04, 32bit).

    AFAIK setting _FILE_OFFSET_BITS=64 on a 32bit-machine forces all file operations to use the 64 functions/datatypes.
    And I assume this behaviour should be default on a 64bit machine.

    What do you get when you print out the size of off64_t?
    Code:
    printf("%d\n", sizeof(off64_t));
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem to read large file.
    By ico303 in forum C Programming
    Replies: 1
    Last Post: 01-22-2011, 11:10 AM
  2. cout output prob large file read
    By kryptkat in forum C++ Programming
    Replies: 2
    Last Post: 02-06-2010, 03:05 PM
  3. Read large amount of numbers from file
    By h3ro in forum C++ Programming
    Replies: 7
    Last Post: 05-10-2007, 10:33 AM
  4. Replies: 4
    Last Post: 01-30-2005, 04:50 AM

Tags for this Thread


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