Thread: appending structure bin file

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    51

    appending structure bin file

    Can anyone please help me with an example of appending the end of a structure in a binary file. For instance, I have an array of 1000 structures, and for instance, so far the user has only wrote to the structure 300. I know hoe to open the file with fopen and fread, but what sort of code would I use to allow the user to write to the next available structure?

    Code:
    struct database {
      char prod[60];
      float cal;
      float carb;
      float prot;
      float fat;  
    };
    
    struct database mainm[1000];

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    instead of opening it with fopen("output.bin", "wb") use fopen("output.bin","ab")

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    instead of opening it with fopen("output.bin", "wb") use fopen("output.bin","ab")
    Mode a opens the file for appending - that is, all new data is written to the end of the file. Any existing data is left alone (not truncuated, like with mode w).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    51

    Still problems, heres the code...

    Ive tried but it will only write write to the structure which I specify, and if I dont specify a number I just get an error when compiling. This is what Im trying to do:


    Code:
    FILE *fp = fopen("output.dat", "ab");
      
          printf("\nEnter the product:");
          fgets(mainm.prod, 60, stdin);
          if ((p = strchr(mainm.prod, '\n')) != NULL) //clears \n
          *p = '\0';
          system("cls");
          printf("\nNow Please Enter The Nutritional Information:");
          printf("\nEnter cal:");
          scanf("%f", &mainm.cal);
          fflushstdin();
          printf("\nEnter carb:");
          scanf("%f", &mainm.carb);
          fflushstdin();
          printf("\nEnter protein:");
          scanf("%f", &mainm.prot);
          fflushstdin();
          printf("\nEnter fat:");
          scanf("%f", &mainm.fat);
          system("cls");
          fflushstdin();
          }

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I assume p is declared as
    Code:
    char *p;
    So you want to input the user's data into a structure, a different one each time? Like this?
    Code:
    int x;
    struct {int i;} s[10];
    for(x = 0; x < 10; x ++) {
        /* read in structure s[x] */
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    OK thanks dwk. What would be the best way to break this loop if the user wanted to quit?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    With break. Like so:
    Code:
    for(/*...*/) {
        /* ... */
        printf("\nAnother? (Y/N) ");
        if(getchar() == 'n') break;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    51

    Problems with this code

    Cn you please tell me what Im doing wrong as its not working properly


    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<conio.h>
    #include<windows.h>
    #include<string.h>
    #include<time.h>
    
    #define EXIT o;
    
    
    //structure to hold all info, final program will contain structures of main meal, snacks
    //and also deserts, using this structure
    
    struct database {
      char prod[60];
      float cal;
      float carb;
      float prot;
      float fat;  
    };
    
    //function prototypes
    
    int full_screen(void);//puts the dos console to full screen when called
    void main_menu(void);
    void about_opt(void);
    void help_opt(void);
    void fflushstdin( void );//clears scanf when called
    void clrscr(void); //clears the screen when called
    
    
    FILE *fp;
    
    struct database *point1;
    
    
    
    
    int main()
    {
        
        
     
     full_screen(); //Puts the dos console into full screen when program is executed
    
    
      struct database mainm[10]; 
      
      main_menu();
    
    
      
      char buf[BUFSIZ];
      char *p;                      
      int i;
      int x;
      FILE *fp;
    
    
      for ( i = 0; i < 10; i++)
      
    
      {
          printf("\nAnother? (Y/N) ");
        if(getch() == 'n') break;
        
    
          printf("\nEnter the product:");
          fgets(mainm[i].prod, 60, stdin);
          if ((p = strchr(mainm[i].prod, '\n')) != NULL)
          *p = '\0';
          system("cls");
          printf("\nNow Please Enter The Nutritional Information:");
          printf("\nEnter cal:");
          scanf("%f", &mainm[i].cal);
          fflushstdin();
          printf("\nEnter carb:");
          scanf("%f", &mainm[i].carb);
          fflushstdin();
          printf("\nEnter protein:");
          scanf("%f", &mainm[i].prot);
          fflushstdin();
          printf("\nEnter fat:");
          scanf("%f", &mainm[i].fat);
          system("cls");
          fflushstdin();
          }
          
          
    fp = fopen("output.dat", "ab");
          
    fwrite(mainm, 1, sizeof(struct database) * 10, fp);
    fclose(fp);
    
    fp = fopen("output.dat", "rb");
    fread (mainm, 1, sizeof(struct database) * 10, fp);
    fclose(fp);
    
    int a;
    
    for ( a = 0; a <10; a ++)
    {
        clrscr();
        
    printf("\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
    printf("\n\nThe nutritional values for ");
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    WORD wOldColorAttrs;
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    GetConsoleScreenBufferInfo(h, &csbiInfo);
    wOldColorAttrs = csbiInfo.wAttributes; 
    
    SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
    printf("%s", mainm[a].prod);
    SetConsoleTextAttribute ( h, wOldColorAttrs);                                        
    printf(" item # %d are:",a);
    printf("\n\nCALORIES - ");
    SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
    printf("%f", mainm[a].cal);
    SetConsoleTextAttribute ( h, wOldColorAttrs);
    printf("\n\nCARBOHYDRATES - ");
    SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
    printf("%f", mainm[a].carb);
    SetConsoleTextAttribute ( h, wOldColorAttrs);
    printf("\n\nPROTEIN - ");
    SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
    printf("%f", mainm[a].prot);
    SetConsoleTextAttribute ( h, wOldColorAttrs);
    printf("\n\nFAT - ");
    SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
    printf("%f\n\n", mainm[a].prot);
    SetConsoleTextAttribute ( h, wOldColorAttrs);
    printf("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
    printf("\n\nPress return for next product...");
    
    getch();
    
    }
    
    }

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What's it not doing? Or doing?

    This code
    Code:
        printf("\nAnother? (Y/N) ");
        if(getch() == 'n') break;
    Should probably go at the end of the for loop, so that the user has to enter at least one structure before being prompted.

    You call main_menu() in main(). main_menu() is not listed here, although you probably have it somewhere. Perhaps main_menu() never returns?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM