allstar
allstar

Reputation: 109

Filing Structure / C

I am getting error after fill information.I dont know where did i make mistake.I want to get information via structure and then save in biodata.txt file.This is just for starting for my program then i will add "delete edit search" options.

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

struct biodata{
       int recno,age;
       char name[20],sex;
       float salary;
}obj;

int main(){
    int addData();
    int showRecord();
    char choice;

    while(1){
        printf("\n\n*****CHOOSE YOUR CHOICE*****\n");
        printf("1) ADD DATA\n");
        printf("2) SHOW RECORD\n");
        printf("Enter your choice : ");

        choice = getche();
        switch(choice){
            case '1' :  
                addData();
                break;
        }
    }
}

int addData(){
     FILE *fp;
     fp = fopen("biodata.txt","w+");

     printf("\n*****ADDING DATA*****\n");
     printf("\nEnter Record No : ");
     scanf("%d",&obj.recno);
     printf("Enter Name : ");

     scanf("%s",obj.name);
     printf("Enter age : ");
     scanf("%d",&obj.age);

     printf("Enter Sex : ");
     scanf("%s",obj.sex);

     printf("Enter Salary : ");
     scanf("%f",&obj.salary);
     fwrite(&obj,sizeof(obj),1,fp);

     fclose(fp);
}

Upvotes: 1

Views: 189

Answers (1)

codaddict
codaddict

Reputation: 454960

You are doing:

printf("Enter Sex : ");
scanf("%s",obj.sex);

But obj.sex is of char type:

struct biodata{
        int recno,age;
        char name[20],sex;
        float salary;
}obj;

Change the scanf to:

scanf("%c",&obj.sex);

Upvotes: 3

Related Questions