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++
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 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
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 .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 ________ /___________
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into...
Write a C++ function that reads a .csv file(file contains rows of string(name) and number) into a vector and loop through that vector and find the max number.
write a Matlab function file to solve system Ax=b by using the output of the function...
write a Matlab function file to solve system Ax=b by using the output of the function lufac2a your function should have inputs f=matrix return from lufac2a, piv=array return by lufac2a and b=right hand side of your system.the only output for your system should be x guideline 1.use the column access for the matrix entries 2. do not create any other matrix in your function-get your data directly from the matrix passed into your function 3.do not use Matlab command designed...
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...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT