second commit
This commit is contained in:
34
wcl1.c
Normal file
34
wcl1.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void count_lines(const char *filename) {
|
||||
FILE *file = fopen(filename, "r");
|
||||
if (file == NULL) {
|
||||
printf("0\t%s\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
int line_count = 0;
|
||||
int ch;
|
||||
|
||||
while ((ch = fgetc(file)) != EOF) {
|
||||
if (ch == '\n') {
|
||||
line_count++;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
printf("%d\t%s\n", line_count, filename);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if(argc < 2){
|
||||
fprintf(stderr, "Usage: %s <file1> [file2 ...]", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
for (int i =1; i <argc; i++) {
|
||||
count_lines(argv[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user