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;
}