Question
Consider the following structure template and variabledefinition: struct rec { char name[30]; double ppg; int years; }team[12]; Suppose there is a file in while the entire variable team hasbeen written in its natural array order. Write a program thataccess the file and reads a single entry of the array asselected by the user. The program should require twoarguments, one being the filename and the second being the numberof the desired entry. The program should not read the entire file;it should use other methods to accessonly the desired entry. Theretrived information should be displayed, and then the programshould exit. Consider the following structure template and variabledefinition: struct rec { char name[30]; double ppg; int years; }team[12]; Suppose there is a file in while the entire variable team hasbeen written in its natural array order. Write a program thataccess the file and reads a single entry of the array asselected by the user. The program should require twoarguments, one being the filename and the second being the numberof the desired entry. The program should not read the entire file;it should use other methods to accessonly the desired entry. Theretrived information should be displayed, and then the programshould exit.
Explanation / Answer
Code: #include struct rec { char name[30]; double ppg; int years; }; int main(int argc, char **argv) { FILE *file; char *file_name; int n; struct rec data; if (argc < 3) return 1; file_name = argv[1]; n = atoi(argv[2]); file = fopen(file_name,"rb"); fseek(file, n * sizeofdata, SEEK_SET); fread(&data, sizeofdata, 1, file); fclose(file); printf("Name: %.30s ",data.name); printf("PPG: %f ",data.ppg); printf("Years: %d ",data.years); return 0; }