linux c 读取文件行数参照wc 系统命令编写的函数如下: #include <fcntl.h> #include <stdlib.h> #define MAXBSIZE 65536 u_long file_wc(char *file) { register u_char *p; register short gotsp; register int ch, len; register u_long linect, charct; int fd; u_char buf[MAXBSIZE]; if (file) { if ((fd = open(file, O_RDONLY, 0)) < 0) return -1; for (gotsp = 1; len = read(fd, buf, MAXBSIZE);) { if (len == -1) return -1; charct += len; for (p = buf; len--;) { ch = *p++; if (ch == '/n') ++linect; if (isspace(ch)) gotsp = 1; else if (gotsp) { gotsp = 0; } } } } return linect; } 测试c #include <stdlib.h> int main(int argc,char *argv[]) { u_long row; row= file_wc("test.unl"); printf("%d/n",row); return 0; }