Write a program to count the number of characters in a file. The program should take the filename through command line arguments.

#include <stdio.h>

int main(int argc , char *argv[])
{
FILE *fp;
int cnt = 0;

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

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

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

while(!feof(fp))
{
fgetc(fp);
cnt++;
}

printf("\nThere are %d characters in \'%s\'", cnt, argv[1]);


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