Hi,
Can someone please give me an example code of how to read a random acess file?
I copy the below code from a book but it read the last record twice. Please show me a better one.
#include <stdio.h>
struct clientDate{
int accNum;
char lastName[15];
char firstName[15];
double balance;
};
int main()
{
FILE *fileptr;
struct clientDate client = {0,"","", 0.0};
if ((fileptr = fopen("myfile.txt", "r") == NULL)
printf("File could not be opened.\n"
else {
printf("%-6s%-16s%11s%10s\n",
"Acct", "LastName", "FirstName", "balance"
while (!feof(fileptr)) {
fread(&client, sizeof(struct clientData), 1, fileptr);
if (client.accNum != 0){
printf(fileptr, "%-6d%-16s%-11s%10.2f\n", client.accNum, client.lastName, client.firstName, client.balance);
}
}
fclose(fileptr);
}
return 0;
}
Can someone please give me an example code of how to read a random acess file?
I copy the below code from a book but it read the last record twice. Please show me a better one.
#include <stdio.h>
struct clientDate{
int accNum;
char lastName[15];
char firstName[15];
double balance;
};
int main()
{
FILE *fileptr;
struct clientDate client = {0,"","", 0.0};
if ((fileptr = fopen("myfile.txt", "r") == NULL)
printf("File could not be opened.\n"
else {
printf("%-6s%-16s%11s%10s\n",
"Acct", "LastName", "FirstName", "balance"
while (!feof(fileptr)) {
fread(&client, sizeof(struct clientData), 1, fileptr);
if (client.accNum != 0){
printf(fileptr, "%-6d%-16s%-11s%10.2f\n", client.accNum, client.lastName, client.firstName, client.balance);
}
}
fclose(fileptr);
}
return 0;
}