|
发表于 2024-8-29 12:05:24
|
显示全部楼层
《零基础入门学习C语言》
小甲鱼 著
立即购买
#define _CRT_SECURE_NO_WARINGS
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 256
long total;
int countLines(const char* filename);
void findALLCodes(const char* patch);
void findALLFiles(const char* patch);
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;
}
void findALLCodes(const char* path)
{
struct _finddata_t fa;
long handle;
char thePath[MAX], target[MAX];
strcpy_s(thePath, path);
if ((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
{
do
{
sprintf_s(target, "%s/%s", path, fa.name);
total += countLines(target);
} while (_findnext(handle, &fa) == 0);
}
_findclose(handle);
}
void findALLFiles(const char* patch)
{
}
void findALLDirs(const char* path)
{
struct _finddata_t fa;
long handle;
char thePath[MAX];
sprintf_s(thePath, path);
if ((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
{
fprintf(stderr, "The path %s is wrong!\n", path);
return;
}
do
{
if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
continue;
if (fa.attrib == _A_SUBDIR)
{
sprintf_s(thePath, " % s / % s", path, fa.name);
findALLCodes(thePath);
findALLDirs(thePath);
}
} while (_findnext(handle, &fa) == 0);
_findclose(handle);
}
int main()
{
char path[MAX] = ".";
printf("计算中...\n");
findALLCodes(path);
findALLDirs(path);
printf("目前你总共写了 %1d 行代码! \n\n", total);
system("pause");
return 0;
}
up好,我照着你的这个输入后检查无误但是它提示我fopen和strcat存在危险,在代码第一行输入#define_CRT_SECURE_NO_WARINGS后依然有报错,想问一下这是怎么回事 |
|