感觉好像打错了。。。。。。
#include <stdio.h>#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#define MAX 256
long total;
int countlines(const char *filename);
int isCode(const char *filename);
void findALLDirs(const char *path);
int countLines(const char *filename)
{
FILE *fp;
int count = 0;
int temp;
if((fp = fopen(filename;"r")) == NULL)
{
fprintf(stderr,"Can not open the file:%s\n", filename);
return 0;
}
while ((temp = fgetc(fp)) != EOF
{
if (temp == '\n')
{
count++;
}
}
fclose(fp);
return count;
}
本帖最后由 jackz007 于 2019-4-13 14:10 编辑
把下面的代码保存成文件 "count.c" 然后编译、运行
#include <stdio.h>
#define MAX 256
int countLines(const char * filename)
{
FILE * fp ;
char s ;
int count = 0 ;
if((fp = fopen(filename , "r")) != NULL) {
while(! feof(fp)) {
fgets(s , MAX , fp) ;
count ++ ;
}
fclose(fp) ;
} else {
fprintf(stderr , "Can not open the file : %s\n" , filename) ;
}
return count ;
}
int main(void)
{
printf("%s : %d lines\n" , "count.c" , countLines("count.c")) ;
}
页:
[1]