Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a random access file

Status
Not open for further replies.

Kendel

Programmer
Apr 24, 2002
1,512
US
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,&quot;&quot;,&quot;&quot;, 0.0};

if ((fileptr = fopen(&quot;myfile.txt&quot;, &quot;r&quot;)) == NULL)
printf(&quot;File could not be opened.\n&quot;);
else {
printf(&quot;%-6s%-16s%11s%10s\n&quot;,
&quot;Acct&quot;, &quot;LastName&quot;, &quot;FirstName&quot;, &quot;balance&quot;);
while (!feof(fileptr)) {
fread(&client, sizeof(struct clientData), 1, fileptr);
if (client.accNum != 0){
printf(fileptr, &quot;%-6d%-16s%-11s%10.2f\n&quot;, client.accNum, client.lastName, client.firstName, client.balance);
}
}
fclose(fileptr);
}
return 0;
}

 
Hello! Anyone in here??? Please reponse !!!
 
Hi:

I see three issues:

Here are two of the changes I made:

here's a code snippet:


fread((char *)&client, sizeof(client), 1, fileptr);
if (client.accNum != 0) {
printf(&quot;%-6d%-16s%-11s%10.2f\n&quot;, client.accNum, client.lastName, client.firstName, cl
ient.balance);

1) I cast the first argument of fread as character. Generally, you don't have to do this, but on some unices I've had a problem in the past.

2) On your original code you had

printf(fileptr, &quot;....

Drop the file pointer. If you want to print to a different file and use fprintf, define an output file.

3) Your code says to loop until the end of the file happens !feof, and print if the accNum is not 0. That's what it's doing. You hit the end of the file, but the while doesn't break until the next loop so the last record prints twice. Your last record is still in memory and it's not 0.

Among the things you can do is add a dummy record at the end of myfile.txt like:

0, &quot;&quot;, &quot;&quot;, 0.0

Regards,


Ed
Schaefer

Oh, and Juck, Chill! Have a beer and you'll feel better in the morning.
 
olded:

If the compiler at least conforms to the 1989 ANSI/ISO standard, the first argument should not be cast to char *. fread() expects a pointer to void and passing a pointer to any type is perfectly fine as it is automatically converted. (maybe the compilers that had a problem with it were pre-ANSI?).

Kendel:

You should be opening the file in binary mode. Add the &quot;b&quot; mode to your mode string:

if ((fileptr = fopen(&quot;myfile.txt&quot;, &quot;rb&quot;)) == NULL)

As to the feof() error in the originally posted code: Did you actually pull this sample out of a book? Geesh! :)
Russ
bobbitts@hotmail.com
 
Thanks for your help.

Olded:
1. I cast the first argument as you suggested.
2. printf(fileptr, &quot;.... -->My bad, I didn't have this in my original code
3. How do I add a dummy record at end of file? I'm just begining to learn C.

rbobbit:
1. I added &quot;rb&quot; mode as you suggested.
2. I did pull out this example from the book &quot;C How to program&quot;

Overall, it still doesn't work. I think if you show me how to add the last dummy record, it might work.

Thanks again for taking your time.

-kendel


 
You have a typo in your sizeof expression (sizeof(struct clientData) instead of sizeof(struct clientDate)).

Try this (untested):

#include <stdio.h>

struct clientDate{
int accNum;
char lastName[15];
char firstName[15];
double balance;
};

int main(void)
{
FILE *fileptr;
/* You don't need to initialize this, but it doesn't hurt */
struct clientDate client = {0,&quot;&quot;,&quot;&quot;, 0.0};

if ((fileptr = fopen(&quot;myfile.txt&quot;, &quot;rb&quot;)) == NULL) {
printf(&quot;File could not be opened.\n&quot;);
} else {
printf(&quot;%-6s%-16s%11s%10s\n&quot;,
&quot;Acct&quot;, &quot;LastName&quot;, &quot;FirstName&quot;, &quot;balance&quot;);
while (fread(&client, sizeof(struct clientDate), 1, fileptr)==1) {
if (client.accNum != 0){
printf(&quot;%-6d%-16s%-11s%10.2f\n&quot;,
client.accNum, client.lastName, client.firstName, client.balance);
}
}
if (ferror(fileptr)) {
puts(&quot;Error reading file&quot;);
}
fclose(fileptr);
}
return 0;
}
Russ
bobbitts@hotmail.com
 
rbobbitt,

Thanks to your help, it works finally. If I want to the first record to be the number of records in the file, how can I do that?

The last record number can be used as number of records but I don't know how to read it.

while(fread(&client, sizeof(struct clientDate), 1, fileptr)==1) {
if (client.accNum == 0)
printf(&quot;%d\n&quot;, lastRecNo or NumofRec here);
else
printf(&quot;%-6d%-16s%-11s%10.2f\n&quot;,
client.accNum, client.lastName, client.firstName, client.balance);
}


 
Never mind. I think I got it.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top