Improved wc

This commit is contained in:
qorg11 2020-06-16 21:28:56 +02:00
parent 971e32a781
commit 9d6db6fc47
No known key found for this signature in database
GPG Key ID: 343FC20A4ACA62B9
1 changed files with 21 additions and 6 deletions

View File

@ -1,12 +1,12 @@
#include <stdio.h>
int
main(void)
void
wc(FILE *file)
{
char c;
int newlines = 0, spaces = 0, bytes = 0;
while((c = getchar()) != EOF)
int newlines, spaces, bytes = 0;
newlines = spaces = bytes = 0;
while((c = fgetc(file)) > 0)
{
bytes++;
if(c == '\n')
@ -14,7 +14,22 @@ main(void)
if(c == ' ')
spaces++;
}
printf("%i %i %i\n",newlines,spaces,bytes);
fclose(file);
}
int
main(int argc, char *argv[])
{
if (argc == 1)
wc(stdin);
else
{
for(int i = 1; i<argc;i++)
{
wc(fopen(argv[i],"r"));
}
}
return 0;
}