昨天才开始学C语言,但做第一节课的课后习题,为什么一直运行不了
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
long total;
int countLines(const char *filename);
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,"/*"), &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;
} 上报错信息啊 Py与C。。。 发表于 2021-5-28 20:22
上报错信息啊
file:///E:/1.png 楚秋惜 发表于 2021-5-28 20:34
? Py与C。。。 发表于 2021-5-28 20:35
?
打字发出来还是图片啊 楚秋惜 发表于 2021-5-28 20:37
打字发出来还是图片啊
贴文字报错信息 E:\测试c\未命名1.cpp In function 'void findAllCodes(const char*)':
49 51 E:\测试c\未命名1.cpp 'fa' was not declared in this scope
E:\测试c\未命名1.cpp In function 'void findALLDirs(const char*)':
63 21 E:\测试c\未命名1.cpp aggregate 'findALLDirs(const char*)::_finddate_t fa' has incomplete type and cannot be defined
Py与C。。。 发表于 2021-5-28 20:38
贴文字报错信息
E:\测试c\未命名1.cpp In function 'void findAllCodes(const char*)':
49 51 E:\测试c\未命名1.cpp 'fa' was not declared in this scope
E:\测试c\未命名1.cpp In function 'void findALLDirs(const char*)':
63 21 E:\测试c\未命名1.cpp aggregate 'findALLDirs(const char*)::_finddate_t fa' has incomplete type and cannot be defined
第81行代码敲错了 findAllCodes(thePath);应该是findALLCodes(thePath);,要和你上面写的函数保持一致
zz学编程 发表于 2021-5-28 22:42
第81行代码敲错了 findAllCodes(thePath);应该是findALLCodes(thePath);,要和你上面写的函数保持一致
我改了之后
E:\测试c\未命名1.cpp In function 'void findAllCodes(const char*)':
49 51 E:\测试c\未命名1.cpp 'fa' was not declared in this scope
E:\测试c\未命名1.cpp In function 'void findALLDirs(const char*)':
63 21 E:\测试c\未命名1.cpp aggregate 'findALLDirs(const char*)::_finddate_t fa' has incomplete type and cannot be defined
82 24 E:\测试c\未命名1.cpp 'findALLCodes' was not declared in this scope
保存文件类型出错了,不是.cpp应该是.c
你用我这个代码,
#include<io.h>
#include<direct.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 256
long total;
int countLines(const char *filename);
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("目前你总共写了%d行代码!\n\n",total);
system("pause");
return 0;
} zz学编程 发表于 2021-5-29 09:35
保存文件类型出错了,不是.cpp应该是.c
你用我这个代码,
我改了,还是不行
E:\测试c\4.c In function 'findAllCodes':
48 52 E:\测试c\4.c 'fa' undeclared (first use in this function)
48 52 E:\测试c\4.c each undeclared identifier is reported only once for each function it appears in
E:\测试c\4.c In function 'findALLDirs':
62 21 E:\测试c\4.c storage size of 'fa' isn't known
这么离谱么,我都可以运行 你用的啥编译器,我用的是devc++ zz学编程 发表于 2021-5-29 14:25
你用的啥编译器,我用的是devc++
我也是啊 48行打错了,自己复查一下 最强废铁h 发表于 2021-5-30 13:43
48行打错了,自己复查一下
我全部又对了一遍
E:\测试c\4.c In function 'findAllCodes':
43 24 E:\测试c\4.c storage size of 'fa' isn't known
E:\测试c\4.c In function 'findALLDirs':
62 21 E:\测试c\4.c storage size of 'fa' isn't known
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 256
long total;
int countLinex(const char *filename);
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 film: %s\n",filename);
return 0;
}
while((temp = fgetc(fp)) != EOF)
{
if (temp == '\n')
{
count++;
}
}
fclose(fp);
return count;
}
void findAllCodes(const char *path)
{
struct _finddate_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 _finddate_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;
}
E:\测试c\4.c In function 'findAllCodes':
43 24 E:\测试c\4.c storage size of 'fa' isn't known
E:\测试c\4.c In function 'findALLDirs':
62 21 E:\测试c\4.c storage size of 'fa' isn't known 楚秋惜 发表于 2021-5-30 16:52
E:\测试c\4.c In function 'findAllCodes':
43 24 E:\测试c\4.c
你还是照着重新敲一遍吧 计算中...
目前你总共写了0行代码! 是什么鬼写完又检查完之后运行就这样子
页:
[1]