More files Handling

Write a program that opens a file called “input.dat”. If the file does not exist, the programme creates it. If it exists already, the programme will simply overwrite it. Your programme should then read data for 5 students from the terminal and write them into the file. Your data should consist of the names of students (20 characters), a student id (10 characters), date of birth (8 characters), gender (1 character) and marital status (1 character).

*/
        #include <stdio.h>
        #include <sys/types.h>
        #include <sys/stat.h>
        #include <fcntl.h>
        #include <unistd.h>
        #include <string.h>

        struct Student{

            char snames[20];
            int  sid[10];
            char sdob[8];
            char sgender;
             char sstatus;
         };

          void inputstud(struct Student *student);
          void printdetails(struct Student *student);
          void writetofile(struct Student student[]);

       int main(){
             const int max=5;
             struct Student s[max];

             int fil=open("input.dat",O_RDWR | O_CREAT);

              inputstud(&s[0]);
              printf("\n");
              printdetails(&s[0]);

            // writetofile(&s[0],1);
              writetofile(s);
            return 0;
         }

void inputstud(struct Student *student){
          printf("Enter Name:\t");
          scanf("%s",student->snames);

          printf("Enter Id:\t");
          scanf("%d",student->sid);
          printf("Enter Date of Birth:\t");
          scanf("%s",student->sdob);
          printf("Enter Gender:\t");
          scanf("%s",&student->sgender);
          printf("Enter Status:\t");
          scanf("%s",&student->sstatus);

}

void printdetails(struct Student *student){
          printf("Name:is %s\n",student->snames);
          printf("Id:is %s\n",student->sid);
          printf("Dateof Birth:is %s\n",student->sdob);
          printf("Gender:is %c\n",student->sgender);
          printf("Status:is %c\n",student->sstatus);

}

void writetofile(struct Student student[]){

    int fil=open("input.dat",O_RDWR | O_CREAT);

   if (fil==-1)
    {
        printf("error has occures");
    }
 else

   {
       printf("Name written is%d  ",write(fil,student->snames,strlen(student->snames)));
       printf("Id written is%d  ",write(fil,student->sid,10));
       printf("Date written is%d  ",write(fil,student->sdob,strlen(student->sdob)));
// printf("Gender written is%d  ",write(fil,student->sgender,1));
//printf("Status written is%d  ",write(fil,student->sstatus,strlen(student.sstatus)));

}

}

Leave a Reply