-
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;
}
-
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.
-
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.
-
>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);
-
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.
-
Place the fclose(FilePtr); after for loop.