index | submit | rank | book

file-size – File size

Write a program that computes the size of a file in bytes and its binary multiples of 1024 (kibibytes, mebibytes and gibibytes).

The filename is provided as a command line argument. Your program should print a single line with:

Here is an example command line session:

$ ./file-size hello.txt
14
$ ./file-size bach--minuet-and-badinerie.flac
13M
$ ./file-size film-the-movie.ogv
207M
$ ./file-size /bin/ls
141K

Multiple quantities should be truncated. A file that is 2.6KiB in size should yield 2K as a result.

If the filename argument is not provided, your program should print error: you must provide a file name to stderr. If the file cannot be opened (e.g.: it does not exist), your program should print error: could not open file "<filename>" with <filename> replaced with the provided file name.

$ ./file-size
error: you must provide a file name
$ ./file-size a/file/that/does/not/exist.txt
error: could not open file "a/file/that/does/not/exist.txt"

Print the actual size of the file not the disk usage.

Scoring

try first: bconv file-read hello-cmd

try next: hconv

index | submit | rank | book

Copyright © 2020-2021 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/file-size