How would you rewrite this???????

This is a discussion on How would you rewrite this??????? within the C Programming forums, part of the General Programming Boards category; I have been working on this program, searched this board and others to try and find out how to rewrite ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    Unhappy How would you rewrite this???????

    I have been working on this program, searched this board and others to try and find out how to rewrite the following code because it doesn't work correctly:


    Code:
    int initialization(struct CUST_INFO custRecs[LIMIT_SIZE],
    	FILE * custFilePtr, 
                    struct ORDER_INFO orderRecs[LIMIT_SIZE],
    	FILE * orderFilePtr, FILE * processFilePtr, int *pageNo);
    Code:
    howMany = initialization(custRecs, processFilePtr, 
    		orderRecs, orderFilePtr, processFilePtr,
    		&pageNo);
    Code:
    int initialization(struct CUST_INFO custRecs[LIMIT_SIZE],
    				   FILE * custFilePtr, struct ORDER_INFO orderRecs[LIMIT_SIZE],
    				   FILE * orderFilePtr, FILE * processFilePtr, int *pageNo)
    {
    	int k = 0;
        char inputBuffer[1024] = {0};
    	while ((k < LIMIT_SIZE) && (fgets(inputBuffer, 1024,
    			custFilePtr)!= NULL))
    		{
    		sscanf(inputBuffer, "%9c %10c %10c %10c %25c %25c %15c %25c\n",
    			&custRecs[k].CustomerId, &custRecs[k].CustomerFirstName,
    			&custRecs[k].CustomerMiddleName, &custRecs[k].CustomerLastName, 
    			&custRecs[k].CustomerAddress, &custRecs[k].CityStateZip,
    			&custRecs[k].CustomerPhoneNo, &custRecs[k].CustomerEmailAddress);
    	}
    	
    	while ((k < LIMIT_SIZE) && (fgets(inputBuffer, 1024,
    			orderFilePtr)!= NULL))
    		{
    		sscanf(inputBuffer, "%9c %i %i %i %f\n",
    			&orderRecs[k].orderId, 
                                                    &orderRecs[k].width, 
                                                    &orderRecs[k].length,
    			&orderRecs[k].carpetCharge,
    			&orderRecs[k].discount);
    		k++;
    		
    		}//end while loop
    	if(k == 0)
    	{
    	
    	printf("Read All Records 1\n");
    	}
    	GetTopLine(processFilePtr);
    	PrintHeadings(pageNo, processFilePtr);
    	return k;
    }//end initialization
    The above code does not read all of the files

    Thanks for any help.
    Imagination at Work

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    1. your first while loop doesn't have a k++ in it, it will always update the first record.

    2. Having fixed that, you will want to reset it back to 0 before starting the 2nd while loop.

    Code:
    int initialization (
            struct CUST_INFO custRecs[LIMIT_SIZE], FILE * custFilePtr,
            struct ORDER_INFO orderRecs[LIMIT_SIZE], FILE * orderFilePtr,
            FILE * processFilePtr, int *pageNo) {
        int k = 0;
        char inputBuffer[1024] = {0};   // doesn't do you any harm not to do this
    
        k = 0;
        while( (k < LIMIT_SIZE) &&
               (fgets( inputBuffer, sizeof(inputBuffer), custFilePtr ) != NULL) ) {
            sscanf( inputBuffer, "%9c %10c %10c %10c %25c %25c %15c %25c",
                &custRecs[k].CustomerId,         &custRecs[k].CustomerFirstName,
                &custRecs[k].CustomerMiddleName, &custRecs[k].CustomerLastName,
                &custRecs[k].CustomerAddress,    &custRecs[k].CityStateZip,
                &custRecs[k].CustomerPhoneNo,    &custRecs[k].CustomerEmailAddress );
            k++;
        }
    
        k = 0;
        while( (k < LIMIT_SIZE) &&
               (fgets( inputBuffer, sizeof(inputBuffer), orderFilePtr ) != NULL) ) {
            sscanf( inputBuffer, "%9c %i %i %i %f", // no need for \n, this is sscanf
                &orderRecs[k].orderId,
                &orderRecs[k].width,
                &orderRecs[k].length,
                &orderRecs[k].carpetCharge,
                &orderRecs[k].discount );
            k++;
    
        }//end while loop
        if(k == 0) {
            printf( "Read All Records 1\n" );
        }
        GetTopLine( processFilePtr );
        PrintHeadings( pageNo, processFilePtr );
        return k;
    }//end initialization
    Which begs the question, what if the two final values of k are different?
    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
    Sep 2001
    Posts
    35

    Unhappy That Made Since but it still does not work.

    Salem,

    Thanks for the reply. It still is not working after making the changes. It reads in the first record of the first data file(cust.dat) and skips the rest of the files of cust.dat and it now reads all of the second data file (order.dat).

    I don't understand why it is doing that.

    Here is the code with the corrections that you made:

    Code:
    int initialization (
            struct CUST_INFO custRecs[LIMIT_SIZE], FILE * custFilePtr,
            struct ORDER_INFO orderRecs[LIMIT_SIZE], FILE * orderFilePtr,
            FILE * processFilePtr, int *pageNo) {
        int k = 0;
        char inputBuffer[1024] = {0};   // doesn't do you any harm not to do this
    
        k = 0;
        while( (k < LIMIT_SIZE) &&
               (fgets( inputBuffer, sizeof(inputBuffer), custFilePtr ) != NULL) ) {
            sscanf( inputBuffer, "%9c %10c %10c %10c %25c %25c %15c %25c",
                &custRecs[k].CustomerId,         &custRecs[k].CustomerFirstName,
                &custRecs[k].CustomerMiddleName, &custRecs[k].CustomerLastName,
                &custRecs[k].CustomerAddress,    &custRecs[k].CityStateZip,
                &custRecs[k].CustomerPhoneNo,    &custRecs[k].CustomerEmailAddress );
            k++;
        }
    
        k = 0;
        while( (k < LIMIT_SIZE) &&
               (fgets( inputBuffer, sizeof(inputBuffer), orderFilePtr ) != NULL) ) {
            sscanf( inputBuffer, "%9c %i %i %i %f", // no need for \n, this is sscanf
                &orderRecs[k].orderId,
                &orderRecs[k].width,
                &orderRecs[k].length,
                &orderRecs[k].carpetCharge,
                &orderRecs[k].discount );
            k++;
    
        }//end while loop
        if(k == 0) {
            printf( "Read All Records 1\n" );
        }
        GetTopLine( processFilePtr );
        PrintHeadings( pageNo, processFilePtr );
        return k;
    }//end initialization
    Imagination at Work

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,849
    > It reads in the first record of the first data file(cust.dat)
    You printed the value of k at the end of the while loop?

    Use feof() and ferror() to determine why the loop exited.
    Code:
      
    if ( ferror(custFilePtr) ) {
        // panic
    }
    Are these files really text files - .dat is not a normal text file extension, it usually means that the file contains binary data, in which case, this is the wrong approach.
    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.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    35

    RE: Are these files really text files

    Salem:

    At the moment, yes they are but once I figure out the problem, I will change all of the "r's" to "rb" and the "w's" to "wb".

    Thanks for all of your help but I think this project is useless. :-(
    Imagination at Work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rewrite C function in C#
    By khdani in forum C# Programming
    Replies: 5
    Last Post: 04-05-2009, 09:44 PM
  2. rewrite to file
    By mystic-d in forum C Programming
    Replies: 1
    Last Post: 12-07-2006, 06:36 AM
  3. rewrite to file
    By mystic-d in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2006, 06:33 AM
  4. Rewrite using functions.
    By g.mail in forum C++ Programming
    Replies: 7
    Last Post: 11-15-2006, 08:06 PM
  5. Contest Sign-Up Thread: Rewrite Mid() from VB
    By Eibro in forum Contests Board
    Replies: 61
    Last Post: 03-25-2003, 09:57 PM

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