Like Tree1Likes
  • 1 Post By xeon321

Binary file - display issue

This is a discussion on Binary file - display issue within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <ctype.h> typedef struct{ char code[6]; int year; char country[26]; } product; void add(); void ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    Binary file - display issue

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    typedef struct{
        char code[6];
        int year;
        char country[26];
    } product;
    
    
    void add();
    void display();
    void del();
    
    
    void add()
    {
        FILE *addPtr;
        product p;
        char anymore = 'Y';
    
    
        addPtr = fopen("product.dat","ab");
    
    
        while(toupper(anymore) == 'Y')
        {
            printf("Product Code: ");
            gets(p.code);
            
            printf("Expired year of the product: ");
            scanf("%d",&p.year);
    
    
            printf("Product country: ");
            fflush(stdin);
            gets(p.country);
    
    
            fwrite(&p,sizeof(product),1, addPtr);
    
    
            printf("Anymore:(Y/N) ?\t");
            scanf("%c",&anymore);
            fflush(stdin);
        }
        fclose(addPtr);
        system("pause");
        system("cls");
    }
    
    
    void display()
    {
        FILE *disPtr;
        product p;
    
    
        disPtr = fopen("product.dat","rb");
    
    
        /*
        fread(&p,sizeof(product),1, productPtr);
    
    
        while (!feof(productPtr));
        {
            printf("Product Code: %s\n",p.code);
            
            printf("Expired year of the product: %d\n",p.year);
    
    
            printf("Product country: %s\n\n",p.country);
            fread(&p,sizeof(product),1, productPtr);
        }
        */
    
    
        
        while ( (fread(&p,sizeof(product),1, disPtr)) == 1);
        {
            printf("Product Code: %s\n",p.code);
            
            printf("Expired year of the product: %d\n",p.year);
    
    
            printf("Product country: %s\n\n",p.country);
    
    
        }
        
        fclose(disPtr);
        system("pause");
        system("cls");
    
    
    }
    
    
    void del()
    {
    }
    
    
    int main()
    {
        int menu;
    
    
        do
        {
            printf("Product Processing System\n");
            printf("=========================\n");
    
    
            printf("1. Add product\n");
            printf("2. Display product\n");
            printf("3. Delete product\n");
            printf("0. Exit\n");
    
    
            printf("Your choice: ");
            scanf("%d", &menu);
            fflush(stdin);
    
    
            system("cls");
    
    
            switch (menu)
            {
                case 1:
                    add();
                    break;
    
    
                case 2:
                    display();
                    break;
    
    
                case 3:
                    del();
                    break;
            }
    
    
        }while (menu != 0);
    
    
    
    
    }
    Issue: It only display the last record rather than all record.
    Attachment: product.dat https://www.dropbox.com/s/xeb6ld7l8c5u844/product.dat

    How do I fix the code to make it display all records if the records is more than 1 in display function?
    Last edited by ulti-killer; 12-03-2012 at 10:05 AM.

  2. #2
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Line 82, ' ; '

    remove it
    ulti-killer likes this.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Why Line 82, ' ; ' can make it display only last record?

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    In this case, add a semicolon behind while() and it will repeat until the condition is false.
    If it's not a loop, execute the statement once.

    same concept as
    Code:
    while(getchar() != '\n');
    which means, keep on "eating" the characters until it encounters '\n'

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    fflush(stdin);
    gets(p.country);
    Half a year on this forum, over 100 posts and still not showing any progress :-(.

    Bye, Andreas

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    Validation

    Yes it does.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary tree display?
    By tennisstar in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2012, 03:57 AM
  2. TTF_Font to GL Display List Kerning issue
    By Alexander Edgar in forum Game Programming
    Replies: 1
    Last Post: 05-10-2012, 01:09 PM
  3. Binary tree display
    By logicwonder in forum C Programming
    Replies: 2
    Last Post: 04-02-2006, 01:14 AM
  4. How to display integer to binary format?
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2005, 12:32 AM
  5. Dual Display issue
    By psychopath in forum Tech Board
    Replies: 8
    Last Post: 02-12-2005, 12:02 PM

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