Open a file handle to each file, use lseek to determine the file's size in bytes and print out the file's size in bytes next to its name. Include a main that does this below.
Please do the above code using C Language.
Solution:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
int main()
{
// Store filename in fn, change it according to your file name
char fn[10] = "z.jpg";
int fd;
// Try to open file, thorw error if file not opened
if( (fd = open(fn, O_RDONLY) ) < 0 ){
perror( "open failed" );
exit( EXIT_FAILURE );
}
// Calculate file size in bytes
off_t size = lseek(fd, 0, SEEK_END);
// Print result
printf("Size of file %s is %ld bytes", fn, size);
// close file
close(fd);
return 0;
}
Output:
Size of file z.jpg is 20145 bytes
Screenshot of code to understand about indentation in code:
PS: Let me know if you have any doubt.
Get Answers For Free
Most questions answered within 1 hours.