Thread: binary file help

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    74

    binary file help

    trying to insert a number 16 into a binary file the binary file contains the following numbers

    0,3,6,9,12,15,18,21,24,27

    the file must remain sorted and im not allowed to append and resort

    my output is

    0,3,6,9,12,15,16,18,16,18,16,18,18

    here is the code
    Code:
     
    #include <stdio.h>
    #include <stdlib.h> 
    
    
    int main(void)
    {
        FILE * fp ; 
        int number ; 
        int numbertemp ; 
        int numbertoinsert = 16 ; 
        
        fp = fopen("numbers_sort.bin" , "r+b" ) ; 
        
        if(fp == NULL)
        {
            printf("Error \n " ) ; 
        }
        
        while(!feof(fp) ) 
        {
            fread(&number , sizeof(int) , 1 , fp ) ; 
            printf("%d\n" , number ) ; 
        }
        
        //read 18 
        fseek(fp , -4*sizeof(int) , SEEK_END) ; 
        fread(&number , sizeof(int) , 1 , fp ) ;
        printf("%d\n" , number ) ; 
        
        //write 16
        fseek(fp , -1*sizeof(int) , SEEK_CUR) ; 
        fwrite(&numbertoinsert , sizeof(int) , 1 , fp ) ;
        fflush(fp);
        //read 21 
        fread(&numbertemp , sizeof(int) , 1 , fp ) ;
        fseek(fp , -1*sizeof(int) , SEEK_CUR) ; 
        
        //write 18
        fwrite(&number , sizeof(int) , 1 , fp ) ;
        fflush(fp);
        //read 24 
        fread(&number , sizeof(int) , 1 , fp ) ;
        fseek(fp , -1*sizeof(int) , SEEK_CUR) ; 
        
        //write 21 
        fwrite(&numbertemp , sizeof(int) , 1 , fp ) ;
        fflush(fp);
        //read 27
        fread(&numbertemp , sizeof(int) , 1 , fp ) ;
        fseek(fp , -1*sizeof(int) , SEEK_CUR) ;
        
        //write 24
        fwrite(&number , sizeof(int) , 1 , fp ) ;
        fflush(fp);
        //write27
        fwrite(&numbertemp , sizeof(int) , 1 , fp ) ;
        fflush(fp);
    
    
        printf("after insert \n " ) ; 
        fseek(fp , 0, SEEK_SET) ;
         while(!feof(fp) ) 
        {
            fread(&number , sizeof(int) , 1 , fp ) ; 
            printf("%d\n" , number ) ; 
        }
        
        
        
        getchar() ; 
        
        return 0 ; 
    }
    what am i doing wrong. i dont see how it can get this output ?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Obviously - you need some algorithm to insert integer into sorted file

    so in pseudocode it would be something like this

    Code:
    int add_number = ...;
    open file
    while(read number)
    {
        if(add_number < number)
        {
            write add_number;
            add_number = number;
        }
    }
    write add_number;
    close file
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    74
    Quote Originally Posted by vart View Post
    Obviously - you need some algorithm to insert integer into sorted file

    so in pseudocode it would be something like this

    Code:
    int add_number = ...;
    open file
    while(read number)
    {
        if(add_number < number)
        {
            write add_number;
            add_number = number;
        }
    }
    write add_number;
    close file

    so i changed it following you pseudocode
    but now i dont get any output at all ?
    Code:
    int main(void)
    {
        FILE * fp ; 
        int number ; 
         
        int numbertoinsert = 16 ; 
        
        fp = fopen("numbers_sort.bin" , "r+b" ) ; 
        
        if(fp == NULL)
        {
            printf("Error \n " ) ; 
        }
        
        while(!feof(fp) ) 
        {
            fread(&number ,sizeof(int) , 1 , fp ) ;
            
            if(numbertoinsert < number )
            {
                fseek(fp , -1*sizeof(int) , SEEK_CUR ) ; 
                fwrite(&numbertoinsert , sizeof(int) , 1  , fp ) ;
                numbertoinsert = number ; 
            }
        }
        fwrite(&numbertoinsert , sizeof(int) , 1  , fp ) ;
        
        fclose(fp) ; 
        
        fp = fopen("numbers_sort.bin" , "rb" ) ; 
        
        if(fp == NULL)
        {
            printf("Error \n " ) ; 
        }
        
        while(!feof(fp) ) 
        {
            fread(&number , sizeof(int) , 1 , fp ) ; 
            printf("%d\n" , number ) ; 
        }
        getchar(); 
        fclose(fp) ; 
        
        return 0 ;
        
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I think main problem lies in the following sentence
    For files open for update (those which include a "+" sign), on which both input and output operations are allowed, the stream shall be flushed (fflush) or repositioned (fseek, fsetpos, rewind) before a reading operation that follows a writing operation.
    fopen - C++ Reference

    So I slightly modified the code and added wrapper to your function (So several numbers could be added one after onother) and at last called your function again to add 16 in the middle of the file - and it worked as desired.

    Code:
    #include <stdio.h>
    
    static int updatefile(int numbertoinsert);
    
    int main(void)
    {
        int i ;
    
        int numberstoinsert[] = {0,3,6,9,12,15,18,21,24,27};
    
        for(i=0;i< sizeof(numberstoinsert)/sizeof(numberstoinsert[0]);i++)
        {
            if(updatefile(numberstoinsert[i]) <0)
                return -1;
            getchar();
        }
    
        return updatefile(16);
    
    }
    
    static int updatefile(int numbertoinsert)
    {
        int number;
        FILE * fp ;
        fp = fopen("numbers_sort.bin" , "r+b" ) ;
    
        if(fp == NULL)
        {
            fp = fopen("numbers_sort.bin" , "w+b" ) ;
            if(fp == NULL)
            {
                printf("Error \n " ) ;
                return -1;
            }
        }
    
        while(fread(&number ,sizeof(int) , 1 , fp ) ==1)
        {
            if(numbertoinsert < number )
            {
                fseek(fp , -1*sizeof(int) , SEEK_CUR ) ;
                fwrite(&numbertoinsert , sizeof(int) , 1  , fp ) ;
                numbertoinsert = number ;
                fseek(fp , 0 , SEEK_CUR ) ;
            }
        }
        fseek(fp , 0 , SEEK_END ) ;
        fwrite(&numbertoinsert , sizeof(int) , 1  , fp ) ;
    
        fclose(fp) ;
    
        fp = fopen("numbers_sort.bin" , "rb" ) ;
    
        if(fp == NULL)
        {
            printf("Error \n " ) ;
        }
    
        while(fread(&number , sizeof(int) , 1 , fp )  ==1)
        {
            printf("%d\n" , number ) ;
        }
        //getchar();
        fclose(fp) ;
    
        return 0 ;
    
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    74
    Thank you that has cleared a lot up. I was convinced it was my compiler.
    In main you have
    Code:
     return updatefile(16)
    I have not seen this before is this to let you know the functions exit status? Ie function returns 0 up success?

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by alien1 View Post
    In main you have
    Code:
     return updatefile(16)
    I have not seen this before is this to let you know the functions exit status? Ie function returns 0 up success?
    since updatefile returns 0 on SUCCESS and -1 on failure it will make main return 0 on SUCCESS and -1 on FAILURE as well
    and so you can check your program EXIT_CODE when running it from the shell script for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-07-2013, 04:33 PM
  2. Replies: 6
    Last Post: 12-06-2013, 11:39 PM
  3. Replies: 9
    Last Post: 12-05-2012, 01:11 PM
  4. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  5. Replies: 12
    Last Post: 06-18-2012, 08:23 AM