Question

Open a file handle to each file, use lseek to determine the file's size in bytes...

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.

Homework Answers

Answer #1

Solution:

  • Look for comments in code to understand it line-by-line.
  • Change the name of the file according to your own need.

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.

Know the answer?
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
Similar Questions
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the contents of every line and store the contents into a string array. Example: .c file that is to be read: #include <stdio.h> int main(){    printf("Hello World");    return ; } _______________(END OF THE FILE)_______________ Therefore, the code will read the contents above and store the contents in every line into an array, such that.... array[0] = "#include <stdio.h>\n" array[1] = "\n" array[2] ="int...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Code a C file, following these instructions: This lab is about getting the input from a...
Code a C file, following these instructions: This lab is about getting the input from a file. Let’s assume the words are provided in one file, passed as an argument to your program. The names of the files are provided as arguments to your program (i.e., they are copied by the shell in the argv variable), and each file is a text file with lots of words. Open the manual page for open() system call and add to your code...
Language: C++ You're given a 1000-line text file, phoneno.txt, where each line consists of a 5-digit...
Language: C++ You're given a 1000-line text file, phoneno.txt, where each line consists of a 5-digit ID# and a phone# in the format of ###-###-####. The data were generated randomly so there might be duplicates in the IDs. You're asked to do the following by using standard library algorithms as much as possible: 1. read the file into a map which has an integer for key (ID#) and a string for value (phone#), this allows the duplicates to be removed....
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance:...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance: ⦁   A base class called Pet ⦁   A mix-in class called Jumper ⦁   A Dog class and a Cat class that each inherit from Pet and jumper ⦁   Two classes that inherit from Dog: BigDog and SmallDog ⦁   One classes that inherit from Cat: HouseCat The general skeleton of the Pet, Dog, and BigDog classes will be given to you (see below, "Skeleton", but...
Python problem: write a main function that ask user for 2 file name, open files and...
Python problem: write a main function that ask user for 2 file name, open files and read the 1st line of each and compare them using hamming function(below is the hamming function I wrote). Just assume the fist line of each file only contains 0s and 1s, such as 0100111101. The main function may have to do some extra work to remove newline characters or other whitespace from the text read from each file. This is hamming function that need...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling...
c# please Create a “Main” program which reads a text file called “collectionOfWords.txt”. Include exception handling to check if the file exists before attempting to open it. Read and print each string to the console. Next modify each word such at the first letter in each word is uppercase and all other letters are lowercase. Display the modified word on the console. Creating a text file: Open up Notepad and type in the following words. Save the file as collectionOfWords.txt...
Please write a program that reads the file you specify and calculates how many times each...
Please write a program that reads the file you specify and calculates how many times each word stored in the file appears. However, ignore non-alphabetic words and convert uppercase letters to lowercase letters. For example, all's, Alls, alls are considered to be the same words. What is the output of the Python program when the input file is specified as "proverbs.txt"? That is, in your answer, include the source codes of your word counter program and its output. <proverbs.txt> All's...
we will be taking data in as a file. you cannot use the data values in...
we will be taking data in as a file. you cannot use the data values in your source file but you must read in everything into variables and use it from there. First we need to include <fstream> at the top of our file. We will need to create an input file stream to work and ifstream. ifstream is a datatype. Create a variable with the datatype being ifstream. Variable is drfine by using the member accessor operator to call...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...