C语言课后作业求助
带你学C带你飞,S1E2课后作业抄完显示无法打开文件是为什么 运行后会这样#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
long total;
int countLines(const char *fliename);
void findAllCodes(const char *path);
void findALLFiles(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;
}
void findAllCodes(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath, target;
strcpy(thePath,path);
if((handle = _findfirst(strcat(thePath,"/*.c"),&fa)) != -1L)
{
do
{
sprintf(target,"%s%s",path,fa.name);
total += countLines(target);
}while (_findnext(handle,&fa) == 0);
}
_findclose(handle);
}
void findALLDirs(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath;
strcpy(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(thePath, "%s%s",path,fa.name);
findAllCodes(thePath);
findALLDirs(thePath);
}
} while (_findnext(handle, &fa) == 0);
_findclose(handle);
}
int main()
{
char path = ".";
printf("计算中...\n");
findAllCodes(path);
findALLDirs(path);
printf("目前你总共写了 %ld 行代码! \n\n", total);
system("pause");
return 0;
} 除非源文件比较多,否则不要发rar
如果源文件比较长(超过几千行几万行) 也不要发这么长的代码几乎没人看
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
long total;
int countLines(const char *fliename);
void findAllCodes(const char *path);
void findALLFiles(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;
}
void findAllCodes(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath, target;
strcpy(thePath,path);
if((handle = _findfirst(strcat(thePath,"/*.c"),&fa)) != -1L)
{
do
{
sprintf(target,"%s%s",path,fa.name);
total += countLines(target);
}while (_findnext(handle,&fa) == 0);
}
_findclose(handle);
}
void findALLDirs(const char *path)
{
struct _finddata_t fa;
long handle;
char thePath;
strcpy(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(thePath, "%s%s",path,fa.name);
findAllCodes(thePath);
findALLDirs(thePath);
}
} while (_findnext(handle, &fa) == 0);
_findclose(handle);
}
int main()
{
char path = ".";
printf("计算中...\n");
findAllCodes(path);
findALLDirs(path);
printf("目前你总共写了 %ld 行代码! \n\n", total);
system("pause");
return 0;
} wp231957 发表于 2022-6-2 08:39
除非源文件比较多,否则不要发rar
如果源文件比较长(超过几千行几万行) 也不要发这么长的代码几乎 ...
谢谢 你把它们放在同一文件夹下了吗 风车呼呼呼 发表于 2022-6-2 14:36
你把它们放在同一文件夹下了吗
在同一文件夹下 找到啦,是有一个filename打成fliename了~ char path = ".\\";
主函数这里改。另外因为countLines函数是通过\n来记录行数的,所以实际上的结果会是每个文件都少计算一行,可以在循环出来之后单独+1
while ((temp = fgetc(fp)) != EOF)
{
if (temp == '\n')
{
count++;
}
}
count++;
当然这样也带来另一问题:当文件为空时,同样会被记作1行。这点你另外加个条件判断一下就行
页:
[1]