69,324
社区成员
发帖
与我相关
我的任务
分享
[User:root Time:07:03:06 Path:/home/liangdong/c]$ make
gcc -g -I./include -c -o src/main.o src/main.c
gcc -o output src/main.o -lpthread -lm -lz
Makefile done.
[User:root Time:07:03:06 Path:/home/liangdong/c]$ ./output
3 10
[User:root Time:07:03:07 Path:/home/liangdong/c]$ cat src/main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count_blank_lines(const char *file, int *ret_blank, int *ret_total) {
FILE *fp = fopen(file, "r");
if (fp == NULL) {
return -1;
}
char line[5];
int blank_count = 0;
int total_count = 0;
int prev_is_new_line = 1;
while (fgets(line, 5, fp) != NULL) {
int len = strlen(line);
int has_new_line = 0;
if (line[len - 1] == '\n') {
total_count++;
has_new_line = 1;
}
if (line[0] == '\n' && prev_is_new_line) {
blank_count++;
}
prev_is_new_line = has_new_line;
}
fclose(fp);
*ret_blank = blank_count;
*ret_total = total_count;
return 0;
}
int main(int argc, char* const argv[]) {
int blank, total;
if (count_blank_lines("data.txt", &blank, &total) == 0) {
printf("%d %d\n", blank, total);
}
return 0;
}
[User:root Time:07:03:13 Path:/home/liangdong/c]$ cat data.txt
1234
1234
4567
aaaaasdf
ddd
dddddd
d
[User:root Time:07:03:15 Path:/home/liangdong/c]$