Thread: using struct array, filling binary file from flat file not getting into bin file.

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    using struct array, filling binary file from flat file not getting into bin file.

    I got it to read to end of file, get all of the data. It diffidently gets put into the struct array in order and complete. It is just not getting written to the binary file. that is being created just not written into it.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define BINFILE "finance.bin"
    
    typedef struct RECORDS_OF_INTREST
    {
        int id;
        char name[200];
        char address [300];
        double acc_bal;    
    }rec_of_intrest;
    
    rec_of_intrest set_of_rec[400];
    
    int main(int argc, const char **argv)
    {
        
        int cnt = 0;
         // read flat file for data input
         FILE *fp;
         //for binary files
         FILE *bfp;
         
         if ( ( fp = fopen(argv[1], "r") ) == NULL )
         {
             printf("No File present\n"
                    "Contact head of state\n"
                    "to see what happened to it.\n"
                    "Phone Number should be in the\n"
                    "yellow pages\n");
            exit(1);
        }
        if (( bfp = fopen(BINFILE, "ab+") ) == NULL)
        {
             printf("No File present\n"
                    "Contact head of state\n"
                    "to see what happened to it.\n"
                    "Phone Number should be in the\n"
                    "yellow pages\n");
            exit(1);
        }  // string with spaces unknown amount %*s %[^\n] end line
        // adding that it no longer gets first line
        
        char buff[1024]; // 4 fgets to null of file
        while ( fscanf(fp, "%d%s%*s%299[^\n]%lf",&set_of_rec[cnt].id,set_of_rec[cnt].name,set_of_rec[cnt].address,&set_of_rec[cnt].acc_bal) 
        && fgets(buff, sizeof buff, fp) != NULL)
        {
            
            fwrite(&set_of_rec[cnt].id, sizeof(int), 1,bfp);
            fwrite(&set_of_rec[cnt].name, sizeof(char),1,bfp);
            fwrite(&set_of_rec[cnt].address, sizeof(char),1,bfp);
            fwrite(&set_of_rec[cnt].acc_bal, sizeof(double),1,bfp);
            
            // it's suppose to just shove all of the struct data into the bin file at one time
            //fwrite(&set_of_rec,sizeof(set_of_rec), 1, bfp);
            printf("cnt %d: %d %s %s %.2lf\n",cnt,set_of_rec[cnt].id,set_of_rec[cnt].name,set_of_rec[cnt].address,set_of_rec[cnt].acc_bal);
            cnt++;
        }
        fclose(fp);
        fclose(bfp);
        
        printf("\nHere comes struct array\n");
        
        for ( int a =0;a<cnt;a++)
            printf("cnt %d: %d %s %s %.2lf\n",cnt,set_of_rec[a].id,set_of_rec[a].name,set_of_rec[a].address,set_of_rec[a].acc_bal);
        
        
        printf("\nDone from struct array\n");
        
        if ( (bfp = fopen(BINFILE, "wb") ) == NULL)
        {
            printf("Someone took it, or I just cannot\n"
                    "access the file. Notify Head of State\n");
            exit(1);
        }
        printf("Id, Name Address, amount in account\n");
        cnt = 0;
        while (fread(&set_of_rec, sizeof(set_of_rec), 1, fp) > 0) {
            printf("\n%d %s %s %.2lf", set_of_rec[cnt].id,set_of_rec[cnt].name,set_of_rec[cnt].address,set_of_rec[cnt].acc_bal);
            cnt++;
        }
        fclose(bfp);
        
    return 0;    
    }
    some of the output off loop and in the struct array itself
    Code:
    cnt 11: 31 jill  jill street 4370.33
    cnt 12: 32 bob  bobs street 1123.44
    cnt 13: 33 sally  sally street 33210.44
    cnt 14: 34 jill  jill street 3340.33
    cnt 15: 35 bob  bobs street 213.44
    cnt 16: 36 sally  sally street 303.44
    cnt 17: 37 jill  jill street 430.33
    
    Here comes struct array
    cnt 18: 20 bob  bobs street 23.44
    cnt 18: 21 sally  sally street 30.44
    cnt 18: 22 jill  jill street 45.33
    cnt 18: 23 bob  bobs street 23.44
    cnt 18: 24 sally  sally street 34.43
    showing it is being read properly and added into the struct array properly. I tried both methods I learned (yesterday) and it is just being stubborn in getting into that bin file. It should work all it is doing is replacing adding one at a time using scanf for each entry.
    Last edited by userxbw; 11-21-2017 at 10:19 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,688
    Thing to try
    Change
    Code:
    &set_of_rec[cnt].id
    to
    Code:
    &(set_of_rec[cnt].id)
    Or use offsetof

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    657
    In the second part of your code, after you close fp and bfp, you open BINFILE for writing and then try to read from fp (which is closed) rather than bfp (which you just opened).

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    well I went back to trying this again
    Code:
        /*
            fwrite(&set_of_rec[cnt].id, sizeof(int), 1,bfp);
            fwrite(&set_of_rec[cnt].name, sizeof(char),1,bfp);
            fwrite(&set_of_rec[cnt].address, sizeof(char),1,bfp);
            fwrite(&set_of_rec[cnt].acc_bal, sizeof(double),1,bfp);
            **/
        // it's suppose to just shove all of the struct data into the bin file at one time
            fwrite(&set_of_rec,sizeof(set_of_rec), 1, bfp);
    

    and well guess what it started working. Now on to the next stage of this project.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by christop View Post
    In the second part of your code, after you close fp and bfp, you open BINFILE for writing and then try to read from fp (which is closed) rather than bfp (which you just opened).
    that might have been it as it was written wb+ and still not working, I just went into hit and miss mode. now it's (bfp = fopen(BINFILE, "ab+") and I changed back to the one liner to add it to the file and it is now working.
    will it keep working? time will tell.

    (bfp = fopen(BINFILE, "rb+") is working too on that last open of bfp

    Code:
    cnt 36: 53 bob  bobs street 213.44
    cnt 36: 54 sally  sally street 303.44
    cnt 36: 55 jill  jill street 430.33
    
    Done from struct array
    Id, Name Address, amount in account
    
    20 bob  bobs street 23.44
    21 sally  sally street 30.44
    22 jill  jill street 45.33
    Last edited by userxbw; 11-21-2017 at 10:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing an array of struct to a binary file
    By alien1 in forum C Programming
    Replies: 6
    Last Post: 03-19-2015, 02:33 PM
  2. Need help filling list array from file
    By jstout in forum C Programming
    Replies: 4
    Last Post: 04-29-2011, 02:04 PM
  3. filling array from file
    By Thegame16 in forum C Programming
    Replies: 20
    Last Post: 12-10-2010, 09:16 PM
  4. Reading flat file and generating tagged file
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 03-24-2006, 08:29 AM
  5. filling an array from a file using fscanf...
    By stodd04 in forum C Programming
    Replies: 2
    Last Post: 03-16-2005, 08:55 AM

Tags for this Thread