Determine the size of a file in the following cases i) if the size is less than 1 KB then find out the size in bytes. ii) if the size is less than 1 MB then find out the size in kilobytes. iii) if the size is less than 1 GB then find out the size in megabytes.

#include <stdio.h>

#define BYTE 1UL
#define KB (BYTE * 1024UL)
#define MB (KB * 1024UL)
#define GB (MB * 1024UL)

int main(int argc , char *argv[])
{
FILE *fp;
unsigned long int sz;

if(argc != 2)
{
printf("\nToo few arguments. You must specify the filename.");
return 1;
}

fp = fopen(argv[1], "rb");

if(fp == NULL)
{
printf("\nUnable to open \'%s\'", argv[1]);
return 1;
}

fseek(fp, 0, SEEK_END);
sz = ftell(fp);

if(sz < KB)
printf("\nSize of %s is %lu bytes", argv[1], sz);
else if(sz >= KB && sz < MB)
printf("\nSize of %s is %lu kilobytes and %lu bytes",
argv[1], sz/KB, sz - ((sz/KB) * KB));
else if(sz >= MB && sz < GB)
printf("\nSize of %s is %lu megabytes %lu kilobytes and %lu bytes",
argv[1], sz/MB, (sz - ((sz/MB) * MB))/KB,
sz - (((sz/MB) * MB) + ((sz - ((sz/MB) * MB))/KB) * KB));
else
printf("\nSize of %s is %lu gigabytes", argv[1], sz/GB);

fclose(fp);

return 0;
}

No comments:

Post a Comment

kiss on google ads if you are anonymous because your ip is trackable.thank you.

......from.admin