c program with binary file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jhansi
    New Member
    • Feb 2008
    • 6

    c program with binary file

    create a binary file and insert the records and query on those inserted record..
    queries are: query based on search by name,search by age and search by designation.


    the problem is i have created a binary file but it is not storing records permanently. i am using binary file for the first time. plz help me to know how to store records permanently . sending my code

    #include<stdio. h>
    #include<conio. h>
    #include<string .h>
    struct record
    {
    char name[50];
    int age;
    char designation[50];
    }record ;

    void main()
    {
    char name1[50], ch;
    // char name[50];
    char designation[50];
    int age1,a,age2;
    struct record r[1];
    FILE *f;
    int i,n,j;
    clrscr();
    f=fopen("c:\\in put.dat","wb");
    if(!f)
    {
    printf("unable to open binary file for reading \n");
    return;
    }
    // fread(&r,sizeof (r),1,f);
    fwrite(&r[a],sizeof(r[a]),1,f);
    printf("Enter the option:\n 1.data entry:\n 2.query:\n");
    scanf("%d",&i);

    switch(i)
    {
    case 1:
    {
    printf("Enter number of records\n");
    scanf("%d",&n);
    // printf("enter the name\n");
    // fgets(name,50,s tdin);
    for(a=1;a <=n; a++)
    {

    printf("Enter name\n");
    scanf("%s",r[a].name);
    // fgetc(r[a].name);
    //gets(r[a].name,50,f);
    printf("Enter age\n");
    scanf("%d",&r[a].age);
    //fgets(r[a].age,1,stdin)
    printf("Enter designation\n") ;
    scanf("%s",r[a].designation);
    // gets(r[a].designation,50 ,f);
    // fprintf(f,"%s %d %s\n", r[a].name,r[a].age,r[a].designation);
    }
    // fwrite(&r[a],sizeof(r[a]),1,f);
    // break;
    }
    case 2:
    {
    printf("Enter the option:\n a.search by name:\n b.search by age:\n c.search by designation:\n" );
    scanf("%s",&ch) ;
    switch(ch)
    {
    case 'a':
    {
    printf("\n Enter name\n");
    scanf("%s",name 1);
    printf("Name\t\ t Age\t\t Designation\t\t \n");
    printf("--------------------------------------------\n");
    for(i=0;i<=n;i+ +)
    {
    // if(strcmp(name1 ,r[i].name)==0)
    // {
    // for(j=0;j<=n;j+ +)
    // {

    if(strncmp(r[i].name,name1,str len(name1))==0)
    {

    printf("%s\t\t %d\t\t %s\n", r[i].name,r[i].age,r[i].designation);
    }
    }

    printf("--------------------------------------------\n");

    break;
    }
    case 'b':
    {
    printf("\n Enter age1\n");
    scanf("%d",&age 1);
    printf("Enter age2\n");
    scanf(" %d",&age2);
    printf("Name\t\ t Age\t\t Designation\t\t \n");
    printf("--------------------------------------------\n");

    for(a=0;a<=n;a+ +)
    {
    if(r[a].age>=age1 && r[a].age<=age2)
    {
    printf("%s\t\t% d\t\t%s\n",r[a].name,r[a].age,r[a].designation);
    }
    // else
    // printf("The name is not in the list of records\n");
    }
    printf("--------------------------------------------\n");
    break;
    }
    case 'c':
    {
    printf("\n Enter designation\n") ;
    scanf("%s",desi gnation);

    printf("Name\t\ t Age\t\t Designation\t\t \n");
    printf("--------------------------------------------\n");
    for(i=0;i<=n;i+ +)
    {
    if(strncmp(r[i].designation,de signation,strle n(designation)) ==0)
    {
    printf("%s\t\t %d\t\t%s\n",r[i].name,r[i].age,r[i].designation);
    }
    }
    printf("--------------------------------------------\n");
    break;
    }
    }
    // default: printf("invalid choice\n");
    // break;
    }
    }
    getch();
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You can't read from a file rhat is write-only. I see the "wb" but I don't see where you re-open the file as readable.

    Comment

    • Simonius
      New Member
      • Feb 2008
      • 47

      #3
      You need to do a fclose so that the file's saved and a new fopen for rb or wb+.

      Comment

      Working...