forked from KIX/lab1_git
66 lines
1.2 KiB
C
66 lines
1.2 KiB
C
#include <stdio.h>
|
|
#include <ctype.h>
|
|
int countLines(const char* fname);
|
|
int countBytes (const char* fname);
|
|
int countWords (const char* fname);
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int nlines = 0;
|
|
int nbytes = 0;
|
|
int nwords = 0;
|
|
for(int i=1; i < argc; i++)
|
|
{
|
|
nlines = countLines(argv[i]);
|
|
nbytes = countBytes(argv[i]);
|
|
nwords = countWords(argv[i]);
|
|
printf("%s: lines - %d, bytes - %d, words - %d\n", argv[i], nlines, nbytes, nwords);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int countLines(const char* fname)
|
|
{
|
|
FILE* file = fopen(fname, "r");
|
|
int lines = 0;
|
|
char buffer[1024];
|
|
while (fgets(buffer, sizeof(buffer), file) != NULL)
|
|
{lines++;}
|
|
fclose(file);
|
|
return lines;
|
|
}
|
|
|
|
int countBytes (const char* fname)
|
|
{
|
|
FILE* file = fopen(fname, "r");
|
|
int bytes = 0;
|
|
int c;
|
|
while ((c = fgetc(file)) != EOF)
|
|
{
|
|
bytes++;
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
int countWords (const char* fname)
|
|
{
|
|
FILE* file = fopen(fname, "r");
|
|
int words = 0;
|
|
int c;
|
|
int is_in_word = 0;
|
|
while ((c = fgetc(file)) != EOF)
|
|
{
|
|
if (isspace(c))
|
|
{is_in_word = 0;}
|
|
else
|
|
{
|
|
if (is_in_word == 0)
|
|
{
|
|
words++;
|
|
is_in_word = 1;
|
|
}
|
|
}
|
|
}
|
|
return words;
|
|
}
|