Thread: Lopping through binary file to read information

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Lopping through binary file to read information

    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:

    Code:
    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:

    Code:
    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:

    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.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Why not use the return value of fread() to control the loop instead of the return value of getc()? Something like:

    Code:
    while (fread(&phonebook,sizeof(struct record),1,ptr_myfile) == sizeof(struct record))
    {

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    12
    Don't use getting characters to check for EOF in this situation, otherwise you need to seek back (or "unget" the character, especially if you're dealing with stdin since seeking probably won't work if stdin is a console) if it's not the end of the file.

    You can check the return value of fread; or you can use the feof function (though the EOF flag does not get set until you try and read beyond the end of the file, so code using feof will need to account for this).

    See fread - C++ Reference and feof - C++ Reference .
    Last edited by Time Traveler; 01-25-2012 at 08:40 AM.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    ahh yeah

    ahh yeah. Using the return value of the fread by just saying
    Code:
    while (fread(......))
    {
    printf(.....)
    ...
    ...
    }
    Thanks again for the help

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by jimblumberg View Post
    Why not use the return value of fread() to control the loop instead of the return value of getc()? Something like:

    Code:
    while (fread(&phonebook,sizeof(struct record),1,ptr_myfile) == sizeof(struct record))
    {
    This should be
    Code:
    while (fread(&phonebook,sizeof(struct record),1,ptr_myfile) == 1)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read binary file
    By larne in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2009, 05:21 AM
  2. Binary read from file
    By dereach in forum C Programming
    Replies: 5
    Last Post: 02-27-2008, 03:34 PM
  3. Read binary file
    By Ken JS in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2007, 11:12 AM
  4. Read a binary file
    By Sue Paterniti in forum C Programming
    Replies: 8
    Last Post: 04-29-2002, 02:36 AM

Tags for this Thread