Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

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

Options
  • 25-01-2012 2: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 Posts: 1,994 ✭✭✭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 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