Thread: Structure Problem

  1. #1
    Registered User Generator's Avatar
    Join Date
    Aug 2001
    Posts
    238

    Structure Problem

    I'm using a structure to store first and last name from a sequential access file. I can scan them in and believe I am loading them into the structure correctly. The problem I am having is swapping the output using printf. If you look in my switch under case 1, you will see that I am trying to print the informatino in my structure in reverse order. I have tried a number of different ways and this is my latest attempt. Can you see what I am doing wrong? I have looked over it for too long.

    Code:
    #include <stdio.h>
    
    	FILE * FilePtr;
    
    int main()	{
    	
    	/*  This creates a client structure.  It is 
    		used to store client information.  When we
    		read from the file we will store our values in 
    		the array structure */
    
    	struct Client
    	{
    		char f_Name[15];
    		char l_Name[15];
    	} Record[5];	//Creates a structure array on client_record
    	
    
    	int i,number;
    
    	/*  Lets open the client.dat file and see if we have opened
    		it correctly... */
    
    	FilePtr = fopen("client.dat", "r");
    	if(FilePtr == NULL)
    	{
    		printf("Unable to access the disk file specified\n");
    		exit(1);
    	}
    		
    
    	/* If it was done correctly, we will attempt to load our structure array
    		with information.  */
    
    	else
    	{
    		printf("client.dat has been opened successfully!!!\n\n");
    		printf("Reading File, printing output...\n\n");
    		
    		for( i = 0; i < 5; i++)
    		{	
    			while(fscanf(FilePtr, "%s %s", Record[i].f_Name, Record[i].l_Name) != EOF)
    			printf("%s, %s \n", Record[i].l_Name, Record[i].f_Name);
    			fclose(FilePtr);
    		}
    
    	}
    
    	printf("\n\nWould you like to switch the name order from: \n");
    	printf("Last Name, First to  First Name, Last? \n");
    	printf("Press 1 for Yes, and 2 for No. \n");
    
    	scanf("%d", &number);
    	
    	switch(number)
    	{
    		case 1:
    
    			printf("Swapping...\n");
    				
    			
    				printf("%s, %s \n", Record[4].f_Name, Record[4].l_Name);
    				
    				printf("%s, %s \n", Record[3].f_Name, Record[3].l_Name);
    				
    				printf("%s, %s \n", Record[2].f_Name, Record[2].l_Name);
    				
    				printf("%s, %s \n", Record[1].f_Name, Record[1].l_Name);
    				
    				printf("%s, %s \n", Record[0].f_Name, Record[0].l_Name);
    			
    			break;
    
    		case 2:
    
    			printf("Nevermind...\n");
    			break;
    
    		default:
    			printf("Please enter a valid character!\n");
    			break;
    	}
    
    	return 0;
    }
    What's a matter you no like peppi?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Look at this loop (well, these two loops actually):
    Code:
      for (i = 0; i < 5; i++)
      {
        while (fscanf(FilePtr, "%s %s", Record[ i ].f_Name, Record[ i ].l_Name) == 2)
          printf("%s, %s \n", Record[ i ].l_Name, Record[ i ].f_Name);
        fclose(FilePtr);
      }
    You're problem is in there. Follow the flow of the two loops (there's in a hint in there ), and you'll find your problem.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Generator's Avatar
    Join Date
    Aug 2001
    Posts
    238
    I don't understand. Are you saying that my EOF argument is somehow messing up the rest of the operation? If I compile, and read in my data. The first part prints out fine. It seems to be storing it into my structure array fine. But when I get to the first select structure, case 1. It throws out a bunch of garbage, and the last record.

    I should be able to access my array, as I have done. Unless I am not throwing the data into the array correctly. Can you be more specific.
    What's a matter you no like peppi?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >It seems to be storing it into my structure array fine.

    No, it's not. Let's look at your version (I added the braces for clarity).
    Code:
    for( i = 0; i < 5; i++)
    {	
    	while(fscanf(FilePtr, "%s %s", Record[i].f_Name, Record[i].l_Name) != EOF)
    	{
    		printf("%s, %s \n", Record[i].l_Name, Record[i].f_Name);
    	}
    	fclose(FilePtr);
    }
    First time through the for loop: read the entire file into Record[0] in the while loop, printing each record as it is read, then close the file.
    Second time through the for loop: attempt to read from the closed file pointer, leave junk in Record[1].
    Third time through the for loop: attempt to read from the closed file pointer, leave junk in Record[2].
    etc.

    Maybe try a single-loop version like this.
    Code:
    for ( i = 0; i < 5; i++ )
    {
       if ( fscanf(FilePtr, "%s %s", Record[i].f_Name, Record[i].l_Name) != 2 )
       {
          break;
       }
       printf("%d, %s, %s \n", i, Record[i].l_Name, Record[i].f_Name);
    }
    fclose(FilePtr);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User Generator's Avatar
    Join Date
    Aug 2001
    Posts
    238
    Ahh I understand now. Thanks for your help guys. I'm going to have to work on my loops. I just couldn't see how it was looping, or how the fscanf worked. I have more experience working with VB so I was looking at it the wrong way. Thanks again.
    What's a matter you no like peppi?

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Place the fclose(FilePtr); after for loop.
    Saravanan.T.S.
    Beginner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 09:53 AM
  2. Problem with structure and class
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2007, 02:30 AM
  3. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 09:35 AM
  4. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 10:52 PM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 02:53 PM