Try this "fixed" version of your code. There are still some things in it I wouldn't recommend, like using a global FILE* variable for one thing. To get it to work, I did these things:
- Incremented the count variable in the loop in Open_file().
- Moved the return(0) statement in Open_file(). It was inside the loop.
- Used fprintf() to do the writing in Write_To_Output().
- fclose()'d the input file.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *FilePtr;
// define 2 dimensional array//
struct Clients
{
char Lname[15];
char Fname[15];
int count;
} Record[5];
int Open_File(void);
int Write_to_Output(void);
int main(void)
{
Open_File();
Write_to_Output();
return(0);
}
int Open_File(void)
{
int count;
count = 0;
if ((FilePtr = fopen("client.dat", "r")) == NULL) //open client.dat as read//
{
printf("\n\nError the file could not be opened.");
exit(1);
}
while (fscanf(FilePtr, "%s", Record[count].Lname) != EOF)
{
fscanf(FilePtr, "%s", Record[count].Fname);
printf("%s %s\n", Record[count].Fname, Record[count].Lname);
count++;
}
fclose(FilePtr);
return(0);
}
int Write_to_Output(void)
{
int count;
if ((FilePtr = fopen("Output.dat", "w")) == NULL)
{
printf("Error : File could not be opened.");
exit(1);
}
for (count = 4; count >= 0; count--)
{
fprintf(FilePtr, "%s %s\n", Record[count].Fname, Record[count].Lname);
}
fclose(FilePtr);
return(0);
}