Thread: What is wrong with these sscans?

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

    What is wrong with these sscans?

    I am trying to read two files( cust.dat and order.dat)
    The first sscan will read the first record of the first data file, skips the rest of the files and then it reads all of the second records of
    the second data file (order.dat). :-(

    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);
    		printf("Read All Records");
    		k = 1;
    		k++;
    		
    		}//end while loop
    	GetTopLine(processFilePtr);
    	PrintHeadings(pageNo, processFilePtr);
    	return k;
    }//end initialization
    Imagination at Work

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > 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);
    <

    When you sscanf a string, you search the string for the data type and put it into a specified variable defined in the second+ parameters of the sscanf function. Well, you are search inputBuffer and then recognizing the characters and you want to put those characters into the specified variables. When you do this, you have to use the address of (&) operator because you have to pass the variables by reference so the function can change them. What you did wrong was that you passed the variable by value so you weren't changing the variable. It should be this:
    Code:
    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);
    Understand?

    --Garfield the Great
    1978 Silver Anniversary Corvette

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Sorry, the output of the reply was a little screwed up. It should look like this:
    Code:
    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);
    1978 Silver Anniversary Corvette

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

    It still doesn't work

    I knew that. Slap self up side the head. I made those changes
    and it still doesn't work.

    Here it is again with the changes.

    also here is the definition and the calling function:


    Code:
    int initialization(struct CUST_INFO custRecs[LIMIT_SIZE], *pageNo, FILE * custFilePtr, struct ORDER_INFO orderRecs[LIMIT_SIZE], FILE * orderFilePtr, FILE * processFilePtr,
    int *pageNo,
    				   int *pageNo);
    Code:
    howMany = initialization(custRecs, processFilePtr, orderRecs, orderFilePtr, processFilePtr, &pageNo);
    Code:
    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);
    		printf("Read All Records");
    		k = 1;
    		k++;
    		
    		}//end while loop
    	GetTopLine(processFilePtr);
    	PrintHeadings(pageNo, processFilePtr);
    	return k;
    }//end initialization
    Imagination at Work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM