Binary file input, and reallocating

This is a discussion on Binary file input, and reallocating within the C Programming forums, part of the General Programming Boards category; Im trying to read in integers from a binary file and then if its more than my capacity (cap) then ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    69

    Binary file input, and reallocating

    Im trying to read in integers from a binary file and then if its more than my capacity (cap) then it reallocates. but for some reason it's only printing 1 number. Here is my code and output.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
            int cap = 10;
            int *arr = malloc( sizeof(int) * cap);
            int i;
            int num_items = 0;
            FILE * fptr;
    
            fptr = fopen("pewp.dat", "r");
    
            num_items = fread ( arr, sizeof(int), cap, fptr);
    
            while(num_items == cap)
            {
                    cap+=10;
                    arr = realloc(arr, sizeof(int) * cap);
                    num_items = fread ( arr, sizeof( int ), cap, fptr );
                    printf("num_items = %d\n", num_items);
            }
    
            fclose( fptr );
            printf("array = ");
            for( i=0; i<num_items; ++i)
            {
                    printf("%d ", arr[i]);
            }
            printf("\n");
    
            free ( arr );
            return 0;
    
    }
    output
    Code:
    num_items = 1
    array = 0

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    365
    Apparently its only reading one record in successfully.

    Also the realloc is a little flawed, you are basically reading in 10 records, then expanding the buffer to 20 and reading in 20 more records which would in fact require a buffer of 30 records at that point (10 for the prior and 20 for the next) and so on...I assume you probably only want to read 10 new records each time and append those to the existing array, right? That's not happening now.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Can you please tell me how many integer is there in the file "pewp.dat" ?

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    here is my write for pewp.dat
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
        int arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
        FILE * fptr = fopen ( "pewp.dat", "w" );
    
        fwrite ( arr, sizeof ( int ), 11, fptr );
    
        fclose ( fptr );
        return 0;
    }
    Quote Originally Posted by nonpuz View Post
    I assume you probably only want to read 10 new records each time and append those to the existing array, right?
    and yes i want to increment my array thats supposed to be taking in the number by ten each time.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    You are having 11 elements , at the first read itself you got 10 elements , for the second fread you have remaining one element thats why you got num_items as 1. if you want the num_items as 11. then add the previous value of the num_elements

    Code:
     num_items = num_items+ fread ( arr, sizeof( int ), cap, fptr );
                    printf("num_items = %d\n", num_items);

    Your whole code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
            int cap = 10;
            int *arr = malloc( sizeof(int) * cap);
            int i;
            int num_items = 0;
            FILE * fptr;
    
            fptr = fopen("pewp.dat", "r");
    
            num_items = fread ( arr, sizeof(int), cap, fptr);
                    printf("verify num_items = %d\n", num_items);  // print for the verification 
                    //exit(0);
    
            while(num_items == cap)
            {
                    cap+=10;
                    arr = realloc(arr, sizeof(int) * cap);
                    num_items = num_items+ fread ( arr, sizeof( int ), cap, fptr );
                    printf("num_items = %d\n", num_items);
            }
    
            fclose( fptr );
            printf("array = ");
            for( i=0; i<num_items; ++i)
            {
                    printf("%d ", arr[i]);
            }
            printf("\n");
    
            free ( arr );
            return 0;
    
    }
    Output:
    Code:
    verify num_items = 10
    num_items = 11
    array = 0 9 8 7 6 5 4 3 2 1 0

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    that worked on making num_items = 11 but my array is printing 0 9 8 7 6 5 4 3 2 1 0 and i want it should be 10 9 8 .... 1 0

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    sine arr is a pointer it was re-writed by the second fread function , so we must write after giving some space
    Code:
                    num_items = num_items+ fread ( arr+num_items, sizeof( int ), cap, fptr );
    Changed code
    Code:
    int main()
    {
            int cap = 10;
            int *arr = malloc( sizeof(int) * cap);
            int i;
            int num_items = 0;
            FILE * fptr;
    
            fptr = fopen("pewp.dat", "r");
    
            num_items = fread ( arr, sizeof(int), cap, fptr);
                    printf("verify num_items = %d\n", num_items);
                    //exit(0);
    
            while(num_items == cap)
            {
                    cap+=10;
                    arr = realloc(arr, sizeof(int) * cap);
                    num_items = num_items+ fread ( arr+num_items, sizeof( int ), cap, fptr ); // added num_items with arr
                    printf("num_items = %d\n", num_items);
            }
    
            fclose( fptr );
            printf("array = ");
            for( i=0; i<num_items; ++i)
            {
                    printf(">%d<\n", arr[i]);
            }
            printf("\n");
    
            free ( arr );
            return 0;
    
    }
    output
    Code:
    >< has no special meaning , just to distinguish 
    verify num_items = 10
    num_items = 11
    array = >10<
    >9<
    >8<
    >7<
    >6<
    >5<
    >4<
    >3<
    >2<
    >1<
    >0<
    Last edited by Alexander jack; 03-01-2010 at 10:21 PM.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    Thanks a lot, works noa

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reallocating Memory
    By Booie2k1 in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 06:09 AM

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