0

I want to write a C code to keep record of n number of family members using structure and write to a FILE and read from it and disply on console. Getting problems. Here is the code I have done. Can you help me?

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

struct family
{
    char name[30];
    int age;
};

void main()
{
    struct family *f1,*f2;
    int i,n;
    FILE *fptr;
    fptr=fopen("D:\\sentence.txt","wb");
    printf("Enter number of family members: ");
    scanf("%d",&n);
    f1=(struct family*)malloc(n*sizeof(struct family));
    f2=(struct family*)malloc(n*sizeof(struct family));
    for(i=0;i<n;i++)
    {
        fflush(stdin);
        printf("\nFor member %d",i+1);
        printf("\nEnter name: ");
        gets(f1->name);
        printf("Enter age: ");
        scanf("%f",&f1->age);
    }
    fwrite(f1,sizeof(f1),1,fptr);
    fclose(fptr);

    fptr=fopen("D:\\sentence.txt","rb");
    if(fptr==NULL)
    {
        printf("File could not be opened");
        exit(1);
    }
    fread(f2,sizeof(f2),1,fptr);
    for(i=0;i<n;i++)
    {
        printf("\nName: %s\tAge: %d",f2->name,f2->age);
    }
    fclose(fptr);
    getch();
}
dragosht
  • 3,237
  • 2
  • 23
  • 32
dpraajz
  • 1
  • 2
  • 4
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Jan 05 '16 at 11:11
  • 2
    **1.** `fflush(stdin);` . **2.** `gets` Don't use them . And yes the repeated one _don't cast result of `malloc`_ – ameyCU Jan 05 '16 at 11:13
  • Doesn't work..@ameyCU – dpraajz Jan 05 '16 at 11:40
  • Apart from all of the other comments, `sizeof(f1)` should probably be `sizeof(*f1)`. The same with f2. – marcolz Jan 05 '16 at 12:04
  • Only last entered name will be displayed for all and age will be garbage value..@marcolz – dpraajz Jan 05 '16 at 12:13
  • @DeeprajBhujel That's because you forgot to put the `fread()` inside the `for` loop. – marcolz Jan 05 '16 at 13:05

1 Answers1

0

As was already mentioned you need to change the argument of sizeof(). You get an explanation here: How to write a struct to a file using fwrite? .To get rid of your last problem you need to move fwrite and fread into the loops.

void main()
{
    struct family *f1,*f2;
    int i,n;
    FILE *fptr;
    fptr=fopen("sentence.txt","wb");
    printf("Enter number of family members: ");
    scanf("%d",&n);
    f1=(struct family*)malloc(n*sizeof(struct family));
    f2=(struct family*)malloc(n*sizeof(struct family));
    for(i=0;i<n;i++)
    {
        fflush(stdin);
        printf("\nFor member %d",i+1);
        printf("\nEnter name: ");
        scanf("%s",&f1->name);
        printf("Enter age: ");
        scanf("%d",&f1->age);
        fwrite(f1,sizeof(struct family),1,fptr);
    }
    fclose(fptr);

    fptr=fopen("sentence.txt","rb");
    if(fptr==NULL)
    {
        printf("File could not be opened");
        exit(1);
    }
    for(i=0;i<n;i++)
    {
        fread(f2,sizeof(struct family),1,fptr);
        printf("\nName: %s\tAge: %d",f2->name,f2->age);
    }
    fclose(fptr);
}
Community
  • 1
  • 1