|
|
发表于 2022-8-11 21:13:20
|
显示全部楼层
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <字符串>
#include <stdlib.h>
#include <sys/stat.h>
#define 最大 256
长总计;
int countLines(const char *filename);
int isCode(const char *filename);
void findAllDirs(const char *path);
int countLines(const char *filename)
{
文件 *fp;
整型计数 = 0;
int temp;
if ((fp = fopen(filename, “r”)) == NULL)
{
fprintf(stderr, “无法打开文件: %s\n”, 文件名);
返回 0;
}
while ((temp = fgetc(fp)) != EOF)
{
if (temp == '\n')
{
计数++;
}
}
fclose(fp);
返回计数;
}
int isCode(const char *filename)
{
整型长度;
length = strlen(filename);
if (!strcmp(filename + (length - 2), “.c”))
{
返回 1;
}
还
{
返回 0;
}
}
void findAllDirs(const char *path)
{
DIR *dp;
struct dirent *entry;
struct statbuf;
if ((dp = opendir(path)) == NULL)
{
fprintf(stderr, “path %s is error!\n”, path);
返回;
}
chdir(path);
while ((entry = readdir(dp)) != NULL)
{
lstat(entry->d_name, &statbuf);
if (!strcmp(“.”, entry->d_name) || !strcmp(“..”, entry->d_name))
继续;
如果 (S_ISDIR(statbuf.st_mode))
{
findAllDirs(entry->d_name);
}
还
{
if (isCode(entry->d_name))
{
总计 += 计数线(入门级>d_name);
}
}
}
chdir(“..”);
closedir(dp);
}
int main()
{
字符路径[MAX] = “.”;
printf(“计算中...\n”);
findAllDirs(path);
printf(“目前你总共写了 %ld 行代码!\n\n”, total);
返回 0;
} |
|