Batch Update in C Programming

This is a discussion on Batch Update in C Programming within the C Programming forums, part of the General Programming Boards category; I need to do this Batch Update on a products list. This batch update has to retrieve EVERY product from ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    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.

    Code:
    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");
                orderMainMenu();
            }
        
        
                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");
            orderMainMenu();
        
        }
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Between fwrites and freads, you need fflush.
    Code:
    		while ( fread(&i,sizeof(i),1,fp) == 1 ) {
    			// do your thing
    			fseek(fp,-sizeof(i),SEEK_CUR);
    			fwrite(&i,sizeof(i),1,fp);
    			fflush(fp);
    		}
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    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, 09: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 Cprogramming.com
    Replies: 1
    Last Post: 05-31-2002, 03:50 PM
  4. batch programming
    By *ClownPimp* in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 02-14-2002, 12:41 PM

Tags for this Thread


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