Hi, I've written two functions, one that fill up an array of structures and write it in a file, the other one should sort the structures and put them in a file.
What is wrong??Code:void fill_up_emp(employee y[], FILE *ptr){ while(!(ptr=fopen("employee.txt","w"))) printf("Cannot open file, Retry:\n"); for (int i = 0 ; i < MAX ; i++){ printf("First name: "); scanf("%s",y[i].first_name); fprintf(ptr,"%s\n",y[i].first_name); printf("Last name: "); scanf("%s",y[i].last_name); fprintf(ptr,"%s\n",y[i].last_name); printf("Salary: "); scanf("%lf",&y[i].salary); fprintf(ptr,"%lf",y[i].salary); } fclose(ptr); void selection_sort_stu(student x[], FILE * ptr){ int result; int min; FILE *ptr_up; while(!(ptr=fopen("student.txt","r"))) printf("Cannot open file, Retry:\n"); while(!(ptr=fopen("student_updated.txt","w"))) printf("Cannot open file, Retry:\n"); for (int i = 0 ; i < MAX - 1 ; i++){ min = i; fscanf(ptr,"%s",x[i].first_name); for (int j = i + 1 ; j < MAX; j++){ fscanf(ptr,"%s",x[j].first_name); if ((result = strcmp(x[i].first_name,x[j].first_name)) > 0) min = j; else if (!(result = strcmp(x[i].first_name,x[j].first_name))){ if ((result = strcmp(x[i].last_name,x[j].last_name))>0) min = j; } } fprintf(ptr_up,"%15s %15s %15.2lf\n",x[min].first_name,x[min].last_name,x[min].GPA); fprintf(ptr_up,"%15s %15s %15.2lf\n",x[i].first_name,x[i].last_name,x[i].GPA); } fclose(ptr);



LinkBack URL
About LinkBacks



