lundi 21 septembre 2020

File handling problem, generate automatically other type data in c

Here this code whenever i want to input for student struct data then also a random courseData created with haphazard number, also when give input for courseData some random data(most of the 2 sets) sets created for student struct... erase the struct student grade and cut connection between two structure but the result remain same.

typedef struct student
 {

    int ID;
   float Homework;
   float Classtest;
   float midterm1;
   float midterm2;
   float Final;

}gradelist;

struct courseData{
 char courseID[10];
 int semester;
 int year;
 int enrolllment;
 struct student grade;
};

FILE *file;

int main(){
 int choice;
do{
 printf("\nChoice ---->>>>> ");
      scanf("%d",&choice);
      switch(choice){
      case 1:
           Read();
           break;
      case 6:
           display_all();
           student_display();
           break;
      case 8:
           printf("Thank you");
           exit(1);
           break;
           }
      }
      while(choice!=8);
}


 //insert function
void Insert(){

  struct student info;

  file = fopen("file.txt","a");

  printf("\nEnter details \n\n");
  printf("\nEnter the ID : ");
  scanf("%d",&info.ID);
  printf("\nEnter the Marks of the homework :  ");
  scanf("%f",&info.Homework);
  printf("\nEnter the Marks of the classtest :  ");
  scanf("%f",&info.Classtest);
  printf("\nEnter the Marks of the midterm1 :  ");
  scanf("%f",&info.midterm1);
  printf("\nEnter the Marks of the midterm2 :  ");
  scanf("%f",&info.midterm2);
  printf("\nEnter the Marks of the final :  ");
  scanf("%f",&info.Final);


 fwrite(&info, sizeof(info), 1, file);
 fclose(file);

}

 //Read the data
void Read(){

  struct courseData cinfo;

  file = fopen("file.txt","w");

  printf("\nEnter the courseID : ");
  scanf("%s",cinfo.courseID);
  printf("\nEnter the semeter : ");
  scanf("%d",&cinfo.semester);
  printf("\nEnter the year : ");
  scanf("%d",&cinfo.year);
  printf("\nEnter the enrollment : ");
  scanf("%d",&cinfo.enrolllment);
  printf("\nEnter the grades : ");
  scanf("%s",&cinfo.grade);


 fwrite(&cinfo, sizeof(cinfo), 1, file);
 fclose(file);

 printf("\nThe year is coint : %d",cinfo.year);
}

Also where i asking for txt file, but the files data not as like text.




Aucun commentaire:

Enregistrer un commentaire