fork download
  1. #include<stdio.h>
  2. struct student{
  3. char name[50];
  4. int id;
  5. float cgpa;
  6.  
  7. };
  8.  
  9. int main(){
  10. FILE *myFile1,*myFile2;
  11. myFile1=fopen("data.txt","r");
  12. myFile2=fopen("output.txt","w");
  13. if(myFile1==NULL){
  14. printf("File nor Found!\n");
  15. return 1;
  16. }
  17. struct student stdList[100];
  18. int n,i;
  19. fscanf(myFile1,"%d",&n);
  20. for(i=0;i<n;i++){
  21. fgetc(myFile1);
  22. fgets(stdList[i].name,sizeof stdList[i].name,myFile1);
  23. fscanf(myFile1,"%d %f",&stdList[i].id,&stdList[i].cgpa);
  24.  
  25. }
  26.  
  27. for(i=n-1;i>=0;i--){
  28.  
  29. fprintf(myFile2,"student %d:\n",i+1);
  30. fprintf(myFile2,"Name:%s",stdList[i].name);
  31. fprintf(myFile2,"ID:%d\n",stdList[i].id);
  32. fprintf(myFile2,"cgpa:%f\n",stdList[i].cgpa);
  33. }
  34. fclose(myFile1);
  35. fclose(myFile2);
  36. return 0;
  37.  
  38. }
  39.  
Success #stdin #stdout 0.03s 25920KB
stdin
Standard input is empty
stdout
#include<stdio.h>
struct student{
    char name[50];
    int id;
    float cgpa;

};

int main(){
    FILE *myFile1,*myFile2;
    myFile1=fopen("data.txt","r");
    myFile2=fopen("output.txt","w");
    if(myFile1==NULL){
        printf("File nor Found!\n");
        return 1;
    }
    struct student stdList[100];
    int n,i;
    fscanf(myFile1,"%d",&n);
    for(i=0;i<n;i++){
        fgetc(myFile1);
        fgets(stdList[i].name,sizeof stdList[i].name,myFile1);
        fscanf(myFile1,"%d %f",&stdList[i].id,&stdList[i].cgpa);

    }

for(i=n-1;i>=0;i--){

    fprintf(myFile2,"student %d:\n",i+1);
    fprintf(myFile2,"Name:%s",stdList[i].name);
    fprintf(myFile2,"ID:%d\n",stdList[i].id);
    fprintf(myFile2,"cgpa:%f\n",stdList[i].cgpa);
}
fclose(myFile1);
fclose(myFile2);
return 0;

}