Thread: Binary file I/O

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    11

    Binary file I/O

    Okay, I'll be the first to admit that I'm in a little bit over my head here.
    Got this assignment from school where we're supposed to read from/write to a binary file, more precisely an index covering name, age and weight.

    We are supposed to use structs and arrays to accomplish this task.

    I can not make any sense of this, and have no idea how to get fread to work.
    when I printf the values (e.g. personinfo[0].name) it's all nonsense.

    Am I totally confused in my coding, or is there some sense in it?

    Any help would be very much appreciated, since my teacher is on vacation.


    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct personinfo
    {
      char name[30];
      int age[3];
      float weight[5];
    };
    
    int main (void) {
    
    int i,j;
    
    
    
    printf("How many entries?\n");
    scanf("%d",&j);
    
    FILE *fp;
    fp=fopen("data.bin", "rb");
    
    struct personinfo person[4];
    
    i=0;
      for (i=0;i<j;i++){
      printf("Name:\n");
      scanf("%s",person[i].name);
      fwrite(person[i].name,sizeof(struct personinfo),sizeof(i),fp);
    
      printf("Age:\n");
      scanf("%d",person[i].age);
      fwrite(person[i].age,sizeof(struct personinfo),sizeof(i),fp);
    
      printf("Weight:\n");
      scanf("%f",person[i].weight);
      fwrite(person[i].weight,sizeof(struct personinfo),sizeof(i),fp);
    }
    
    i=0;
    for (i=0;i<j;i++){
      fread(person[i].name,sizeof(struct personinfo),sizeof(i),fp);
      fread(person[i].age,sizeof(struct personinfo),sizeof(i),fp);
      fread(person[i].weight,sizeof(struct personinfo),sizeof(i),fp);
    
    }
    
    
    fclose(fp);
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    fp=fopen("data.bin", "rb");

    rb means open file for reading but you want to write to it, so...

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Maybe your teacher intends you to do something like this:
    Code:
    struct personinfo person;
    for (i=0; i < j; i++){
            printf("Name: ");
            scanf("%s", person.name);
            printf("Age: ");
            scanf("%d", &person.age);
            printf("Name: ");
            scanf("%f", &person.weight);
            fwrite(&person, sizeof(person), 1, fp);
    }
    
    // here you need to close & reopen, or rewind, or something. . .
    // then read the structs back:
    while (!feof(fp)){
            fread(&person, sizeof(person), 1, fp);
    }
    The beauty of using fread & fwrite is that you can dump an entire struct to a file such that (assuming you have no pointers in your struct) when you read the data back you get exactly what you wrote.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    11
    Thanks sugarfree, changed it now.

    Kennedy, your method is undoubtedly a whole lot prettier than mine.
    Though I can't get it to read anything back to me.

    My code now looks as you suggested:

    Code:
    i=0;
      for (i=0;i<j;i++){
      printf("Name:\n");
      scanf("%s",person[i].name);
      printf("Age:\n");
      scanf("%d",&person[i].age);
      printf("Weight:\n");
      scanf("%f",&person[i].weight);
      fwrite(&person,sizeof(person),1,fp);
    }
    
    rewind(fp);
    
    while(!feof(fp)){
      fread(&person, sizeof(person),1,fp);
    }
    
    fclose(fp);
    }
    But when I run the program, after entering the values of my choice, it only displays a blank row, nothing else. Could it be that the write operation failed somehow and that it has nothing to display, or is it because the reading is failing?

    Very thankful for any input!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you opening the file for reading and writing?

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    11
    Thanks Adak, good point, I only opened it in wb.
    After I changed it to wrb though, it just exits the program as soon as i have entered the values of my choice. Any input?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to print out the data, if you want to see it. Just reading it into a struct, won't display it on the screen.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    11
    Ah, sorry for me being slow on the uptake! Now that i print it, it prints the values! Thank you very much!

    Though it does not seem to write anything to the file, but just use the buffer values.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post up your whole code, obviously, something is wrong if nothing is being written out to the file.

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    11
    Here is my code in full as is of now

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct personinfo
    {
      char name[30];
      int age;
      float weight;
    };
    
    int main (void) {
    
    int i,j;
    
    
    
    printf("How many entries?\n");
    scanf("%d",&j);
    
    FILE *fp;
    fp=fopen("data.txt", "wrb");
    
    struct personinfo person[4];
    
    i=0;
      for (i=0;i<j;i++){
      printf("Name:\n");
      scanf("%s",person[i].name);
      printf("Age:\n");
      scanf("%d",&person[i].age);
      printf("Weight:\n");
      scanf("%f",&person[i].weight);
      fwrite(&person,sizeof(person),1,fp);
    }
    
    rewind(fp);
    
    while(!feof(fp)){
      fread(&person, sizeof(person),1,fp);
      printf("%s\n",person[1].name);
    
    }
    
    fclose(fp);
    }

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    while(!feof(fp)){
      fread(&person, sizeof(person),1,fp);
      printf("%s\n",person[1].name);
    
    }
    This is problematic.

    Code:
      struct personinfo p;
     while(  fread(&p,sizeof(p),1,fp) == 1 ) {
      printf("%s\n",p.name);
     }
      // check EOF or read error
    c-faq

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > fwrite(&person,sizeof(person),1,fp);
    You write 4 people each time, not the one you just input.

    > for (i=0;i<j;i++)
    Given your array, I hope you don't type in a number > 4 when you run the code. That would be bad voodoo.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Jul 2010
    Posts
    11
    Bayint:
    Tried changing to your suggestion and got a segmentation fault after entering all the data in the program. Am I missing something?

    Salem:
    I must honestly say that I'm not very up to speed on the fread parameters. Which one is causing this?
    As I understand it, I now write the whole person struct with all it's (3) values, with the size of the whole struct, and one element. Am I wrong in that assumption?

    Best regards

  14. #14
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The way you write to file with fwrite() is not correct.

    Code:
      
      fwrite(&person,sizeof(person),1,fp);     // WRONG!
      fwrite(&person[i], sizeof(person[i]),1,fp);  // RIGHT!
    Edit: Forgot to modify sizeof( xxx).
    Last edited by Bayint Naung; 07-31-2010 at 06:16 PM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    sizeof(person) should evaluate to the size of the array in bytes, so that is wrong as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  3. Binary FIle I/O
    By MethodMan in forum C Programming
    Replies: 5
    Last Post: 03-21-2003, 02:22 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. I/O to binary file
    By pors7 in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2001, 02:27 PM