Question

In: Computer Science

Write the body of the function getFileSize to return the size of an opened file. #include...

Write the body of the function getFileSize to return the size of an opened file.

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

long getFileSize (FILE * f)

{

} int main () { FILE * file = fopen (“myfile.txt”,”r”); long fsize = getFileSize(file); printf ("File Size : %lu\n",fsize); return 0;}

Solutions

Expert Solution

C Program :

_____________________________

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
long getFileSize (FILE * f)
{
    fseek(f, 0L, SEEK_END) ; // fseek to move file pointer byte by byte
    long res = ftell(f);     // ftell gives current position of file pointer
    fclose(f);               // close file pointer
    return res;              // return the result
}
int main()
{
    FILE *file = fopen ("myfile.txt","r");
    long fsize = getFileSize(file);
    printf ("File Size : %lu\n",fsize);
    return 0;
}

_____________________________

Explanation :

fseek() function format is : int fseek(FILE *pointer, long int offset, int origin)

where pointer is the file pointer

offset is number of characters to shift the position relative to origin

origin is the position to which offset is added.

ftell() format is : long ftell(FILE *pointer) , ftell takes file pointer as input and returns the current position of file pointer with respect to beginning of the file.

_____________________________

SCREENSHOTS :

My "myfile.txt" looked like this

The output of program is :

To confirm that the result was correct,I checked for file size using file properties in windows

You can see that the file size is same as returned by the program(in bytes).

_________________________________

Please comment in case you have any doubt.


Related Solutions

Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
Write a function file that accepts each of the four vectors as inputs. The function file...
Write a function file that accepts each of the four vectors as inputs. The function file should plot the data on different graphs in the same window. You need to add axis labels and graph titles. time- 0 5 10 15 20 25 30 A1- 0 7 11 19 15 14 7 A2- 0 10 15 21 16 11 13 A3- 0 9 13 17 22 25 21
Write a python function average_sample_median(n,P), that return the average of 1000 samples of size n sampled...
Write a python function average_sample_median(n,P), that return the average of 1000 samples of size n sampled from the distribution P. * Sample run : print(average_sample_median(10,[0.2,0.1,0.15,0.15,0.2,0.2])) print(average_sample_median(10,[0.3,0.4,0.3])) print(average_sample_median(10,P=[0.99,0.01]) * Expected Output : 3.7855 2.004 1
HW_6b - Read and write a text file. Include the following in the main.cpp file. Add...
HW_6b - Read and write a text file. Include the following in the main.cpp file. Add code so that the numbers that are read from the data.txt file are written to an output           file named:   results.txt After the numbers are written to the file, the following message should be displayed:    The data has been written to the file. Open the results.txt file and verify that the file was written successfully. Please make the code based on C++,...
Write a parameterized function that takes in a file name as a parameter, reads the file,...
Write a parameterized function that takes in a file name as a parameter, reads the file, calculates the factorial of each number, and displays a formatted output as follows: Factorial of 10 = 3628800 Factorial of 5 = 120
Write a class file called lastname_digits.java. In it, write a method to compute and return the...
Write a class file called lastname_digits.java. In it, write a method to compute and return the total instances where the sum of digits of the array equals to the target. Write a demo file called lastname_digits_demo.java. Create an array of random 10 integers between 301 and 999. Also create an integer variable called target that is equal to a value between 5 and 10. Print the answer within this file. Example: If five of the 20 integers are equal to...
In C++ Write a function that took in SIZE and array [size] and counted the number...
In C++ Write a function that took in SIZE and array [size] and counted the number of elements >100
Write a .m function file on MATLAB that reads the parameters stored in the file missile...
Write a .m function file on MATLAB that reads the parameters stored in the file missile data.txt into MATLAB. The function should have the following declaration: function [X0, Y0, Z0, m0, mf, Thmag0, theta, phi, Tburn] = read input( input filename, M id) where input filename is a string variable denoting the name of the file to be read and M_id is an integer which denotes the missile ID. The outputs are the initial position (X0, Y0, Z0), initial and...
Complete this return statement so that it could be used in the function body offind_percentageto fulfill...
Complete this return statement so that it could be used in the function body offind_percentageto fulfill the requirements for the last step's program. Fill in the Blanks — Fill in the blanks return ________ /___________
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT