A binary file stores a list of company’s salaries. Each salary is stored as floa
ID: 3821907 • Letter: A
Question
A binary file stores a list of company’s salaries. Each salary is stored as float. Which code fragment below will read the third salary of the file into a variable sal. The file is already opened using the file description fid.
a. fseek(fid, 0, SEEK_SET);
fread(&sal, sizeof(double), 1, fid);
b. fseek(fid, 0, SEEK_END);
rc = ftell(fid);
fseek(fid, rc - 3*sizeof(double), SEEK_END);
fread(&sal, sizeof(double), 1, fid);
c. fseek(fid, 0, SEEK_SET);
rc = ftell(fid);
fseek(fid, rc - 2*sizeof(double), SEEK_CUR);
fread(&sal, sizeof(double), 1, fid);
d. fseek(fid, 0, SEEK_SET);
fseek(fid, 2*sizeof(double), SEEK_CUR);
fread(&sal, sizeof(double), 1, fid);
e. None of the above
Explanation / Answer
Answer:-
C
fseek(fid, 0, SEEK_SET);
rc = ftell(fid);
fseek(fid, rc - 2*sizeof(double), SEEK_CUR);
fread(&sal, sizeof(double), 1, fid);
This will help to read the third salary of the file into a variable sal.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.