Thread: Batch Update in C Programming

  1. #1
    Registered User
    Join Date
    Dec 2012

    Batch Update in C Programming

    I need to do this Batch Update on a products list. This batch update has to retrieve EVERY product from within the database and get it's "quantity in Order". This "Quantity in Order" then has to be added to the current stock:

    For e.g:

    Current in Stock : 100
    In order: 150

    After the Batch Update they have to be:
    Current in Stock :250
    In Order: 0

    I have done this method (see below) BUT when I run it, it's not updating my Stock and neither my Order.

    void batchUpdate()
            printf ("\n\n\n\n\t\t    ********** Batch Update *******\n \n \n");
            int tempOrder;
            int tempStock;
            int tempAdding;
            if ((pfp = fopen("products.dat","r+b")) == NULL)
                printf ("Error! Cannot Open Products.dat!\n");
                printf ("Returning to Main Menu\n");
                system ("PAUSE");
                while (fread(&p,STRUCTSIZE,1,pfp) == 1)
                    tempOrder = p.pOrder;
                    tempStock = p.pStock;
                    tempAdding = tempOrder + tempStock;
                    p.pOrder = 0;
                    p.pStock = tempAdding;
                    fwrite (&p,STRUCTSIZE,1,pfp);
            fclose (pfp);
            printf ("Orders and Stock Updated!\n");
            printf ("Returning to Main Menu!\n");
            system ("PAUSE");
    I tried using fseek (pfp, -STRUCTSIZE,SEEK_CUR); but that turned my program into an infinite Loop. Also when I tried to use show All products in database, they skipped my first record and just went into an infinite loop showing the last record only. I just have to end up deleting the products.dat file from the system and redo it again.
    Any suggestion? I do believe that there is something wrong with the fseek().
    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Between fwrites and freads, you need fflush.
    		while ( fread(&i,sizeof(i),1,fp) == 1 ) {
    			// do your thing
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Thanks a lot! Worked like a charm!

    so why was it doing an infinite loop!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Batch / FTP
    By bobothesmart in forum A Brief History of
    Replies: 1
    Last Post: 05-31-2002, 03:50 PM
  4. batch programming
    By *ClownPimp* in forum A Brief History of
    Replies: 5
    Last Post: 02-14-2002, 01:41 PM

Tags for this Thread