Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Read and Write the Structure in File using C ( File Handling )

    • 0
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 29.7k
    Comment on it

    File Handling in C:- The File handling is used in c to store the information in file. we can read the data from file and write the data into file for securing your data. Here the below example can show you how to write and read the data of structure in c. we can read entire structure and write the entire structure into a file rather than character by character.

    FWrite:- The Fwrite can write your data into a file. It can write the structure into your file the structure can contain multiple property for the one instance of a record.

    FRead:- The Fread can be read the data from file. it can pic the one instance of structure at a time and display it's record till the End of File is reached.

     

     

    The Below Example can show you how it can be insert the record of employee using structure.

    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    void read();
    void write();
    
    struct record
    {
      char name[30];
      int id;
      float sal;
    }s;
    
    main()
    {
    int ch;
    clrscr();
        while(1)
        {
            printf("\n1:Write Records");
            printf("\n2:Read Records");
            printf("\n3:Exit");
            printf("\n\tEnter Your Choice:- ");
            scanf("%d",&ch);
    
            switch(ch)
            {
                case 1:
                write();
                break;
    
                case 2:
                read();
                break;
    
                case 3:
                exit(1);
    
    
                default:
                printf("\n\tOption not Available\n");
                break;
            }
        }
    
    getch();
    }
    
    void write()
    {
    int i,n=0;
    FILE *fp;
    
        fp=fopen("stu.dat","wb");
        if(fp==NULL)
        {
            printf("can't create file");
            getch();
            exit(1);
        }
        printf("\n\tHow Many Records You Want to Enter:=");
        scanf("%d",&n);
    
        for(i=0;i<n;i++)
        {
    
            printf("\nEnter Employee ID := ");
            scanf("%d",&s.id);
    
            printf("\nEnter Employee Name := ");
            scanf("%s",s.name);
            flushall();
    
            printf("\nEnter the Salary:=");
            scanf("%f",&s.sal);
            printf("\n*****************\n");
    
            fwrite(&s,sizeof(s),1,fp);
    
        }
    fclose(fp);
    }
    
    void read()
    {
    FILE *fp;
    
        fp=fopen("stu.dat","rb");
        if(fp==NULL)
        {
            printf("can't read file");
            getch();
            exit(1);
        }
            while(fread(&s,sizeof(s),1,fp)==1)
            {
                printf("\nEmployee ID := %d",s.id);
                printf("\nEmployee Name := %s",s.name);
                flushall();
                printf("\nSalary:= %f",s.sal);
                printf("\n********************\n");
    
            }
    
    fclose(fp);
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: