Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Looping (and reading) through binary file. (C programming)

  • 25-01-2012 02:05PM
    #1
    Closed Accounts Posts: 11


    Hi,

    I have created a program which reads in information from keyboard into a structure and saves the info into a binary file.

    I can also get the program to read the information back from the binary file and display it. However, when there is more than 1 records of information in the file, reading it back presents a problem.

    I am using the following code to read back from the file:

    fread(&phonebook,sizeof(struct record),1,ptr_myfile);
    printf("First Name: %s\n",phonebook.firstname);
    printf("Surname: %s\n",phonebook.lastname);
    printf("Address: %s\n",phonebook.address);
    printf("Phone Number: %s\n",phonebook.phonenumber);

    If I double the code to this:

    fread(&phonebook,sizeof(struct record),1,ptr_myfile);
    printf("First Name: %s\n",phonebook.firstname);
    printf("Surname: %s\n",phonebook.lastname);
    printf("Address: %s\n",phonebook.address);
    printf("Phone Number: %s\n",phonebook.phonenumber);

    fread(&phonebook,sizeof(struct record),1,ptr_myfile);
    printf("First Name: %s\n",phonebook.firstname);
    printf("Surname: %s\n",phonebook.lastname);
    printf("Address: %s\n",phonebook.address);
    printf("Phone Number: %s\n",phonebook.phonenumber);

    It will read the two records of information in the file. However, that is not practical and I am looking for a way to have it loop until the end of file is reached.

    So, after alot of researching online, I used the code:

    while ((ch=getc(ptr_myfile) != EOF))
    {
    fread(&phonebook,sizeof(struct record),1,ptr_myfile);
    printf("First Name: %s\n",phonebook.firstname);
    printf("Surname: %s\n",phonebook.lastname);
    printf("Address: %s\n",phonebook.address);
    printf("Phone Number: %s\n",phonebook.phonenumber);
    printf("\n");
    }

    While this works, the problem is that it strips each field in the first record of the first character, each field in the second record of the first and second characters, and so on. The problem, I believe, being with the 'getc' function.

    So Im wondering if there is any other way I can read all records in the binary file until the EOF is reached?

    Thanks in advance.

    S.


Comments

  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


    From what I remember fread returns a size_t indiciating the number of records read. If its not equal to the count parameter then it means u hit eof or an error occurred. You can check feof or ferror to see what cause it.


  • Registered Users, Registered Users 2 Posts: 20 lowlifer


    I used to do this:
        f1=fopen("stuff.dat","rb");
        while(!feof(f1)){
    	 fread(&bc,sizeof(struct record),1,f1);
        }
    


  • Closed Accounts Posts: 11 squinchy


    ah yea. didnt use your code, but it did get me thinking that fread returns a value of its own, so I just put a while loop around it like this:

    while(fread(......))
    {
    ...
    ...
    ...
    }

    and it worked a treat.

    Thanks for your help guys.


Advertisement