Question

Write a function that accepts a handle to a binary file and prints the third last record in the file The records in the binary file are salaries of employees in a company. Each record is a double (namely each salary is stored as a double). The function prototype is void print ThirdLast(FILE *fid);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the below code:

thirdLastRecord.c

#include<stdio.h>
//function declaration
void printThirdLast(FILE *fid);
//main method
int main()
{
   //dclared file pointer
   FILE* file;
   //calling the method
   printThirdLast(file);
  
   return 0;
}

void printThirdLast(FILE *fid)

{
   //storing the file value into array
   double array[1000];
   int i=-1;
   double variable;
       //opening file as read mode
fid = fopen("emp.txt","r");
   //reading line by line
       char line[100];
   while( fgets( line,100,fid ) ){
       if( 1==sscanf(line,"%lf",&variable) ){
       //storing the value into array
           //incrementing the counter
           array[++i]=variable;
          
       }
        else
   puts("variable not valid");
   }
   //printing the third last variable from array
   //i will be last, i-1 will be second last and
   //i-2 will be third last
   printf("Third last record %.02f",array[i-2]);


}

emp.txt

34343.0
534344.0
3546.0
747.98
454.756
456.98
23233.56
123.78
3453.445
2344.67

OUTPUT


Third last record 123.78 Process exited after 0.009781 seconds with return value 0 Press any key to continue - -

Add a comment
Know the answer?
Add Answer to:
Write a function that accepts a handle to a binary file and prints the third last...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function named vertical that accepts a string as its parameter and prints each letter...

    Write a function named vertical that accepts a string as its parameter and prints each letter of the string on separate lines. For example, a call of vertical("hey now") should produce the following output: h e y n o w

  • In the processLineOfData, write the code to handle case "H" of the switch statement such that:...

    In the processLineOfData, write the code to handle case "H" of the switch statement such that: An HourlyEmployee object is created using the firstName, lastName, rate, and hours local variables. Notice that rate and hours need to be converted from String to double. You may use parseDouble method of the Double class as follows:               Double.parseDouble(rate) Call the parsePaychecks method in this class passing the HourlyEmployee object created in the previous step and the checks variable. Call the findDepartment method...

  • 5. Write a Python function called readlinesmton that accepts the name of the file (i.e. filename),...

    5. Write a Python function called readlinesmton that accepts the name of the file (i.e. filename), starting line number i.e. m) and ending line number (i.e. n) as a parameter. The function then returns the contents from line number m to n (m <n). If m < 1 then start from line 1. Similarly, if n > number of lines the file, then stop at the last line in the file. Sample Input: readlinesmton("data.txt",5,7) Sample Input: readlinesmton("data.txt", 0,10)

  • Write a program in C language that reads scores and id numbers from a file, finds...

    Write a program in C language that reads scores and id numbers from a file, finds the average score, and assigns each student a grade based on the following rules: Each score > average + 20 gets an A Each score between average + 10 and average + 20 gets a B Each score between average - 10 and average + 10 gets a C Each score between average - 20 and average - 10 gets a D Each score...

  • In the processLineOfData method, write the code to handle case "H" of the switch statement such...

    In the processLineOfData method, write the code to handle case "H" of the switch statement such that: • An HourlyEmployee object is created using the firstName, lastName, rate, and hours local variables. Notice that rate and hours need to be converted from String to double. You may use parseDouble method of the Double class as follows: Double.parseDouble(rate) Call the parsePaychecks method in this class passing the Hourly Employee object created in the previous step and the checks variable. Call the...

  • // includes [1 mark] // structure definition [5 marks] // global variables [2 marks] // function...

    // includes [1 mark] // structure definition [5 marks] // global variables [2 marks] // function prototypes [2 marks] // main function (no menu required – just invoke the function calls to // satisfy the requested program sequence) [5 marks] // Code all necessary functions [25 marks] Q6. [40 MARKS] Write and document a complete C program to satisfy the following requirements. Given a sequential text file that has the following layout where each line is one record containing the...

  • Greeter Write a function that takes in a person's name, and prints out a greeting. The...

    Greeter Write a function that takes in a person's name, and prints out a greeting. The greeting must be at least three lines, and the person's name must be in each line. example: Hello name How are you do name Nice to meet you name Use your function to greet at least three different people. Full Names Write a function that takes in a first name and a last name, and prints out a nicely formatted full name, in a...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • c++ program Notes 1. Your program should use named string constant for the file name to open the file. Do not add any pa...

    c++ program Notes 1. Your program should use named string constant for the file name to open the file. Do not add any path to the file name because the path on your computer is not likely to exist on the computer the instructor uses to grade the program. Points may be deducted if you don't follow this instruction. 2. Split statements and comments longer than 80 characters into multiple lines and use proper indentations for the split portions. 3....

  • C++ 1. Your program should use named string constant for the file name to open the...

    C++ 1. Your program should use named string constant for the file name to open the file. Do not add any path to the file name because the path on your computer is not likely to exist on the computer the instructor uses to grade the program. Points may be deducted if you don't follow this instruction. 2. Split statements and comments longer than 80 characters into multiple lines and use proper indentations for the split portions. (Lab2b.cpp) Write a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT