语法编译没错误,运行是说出错了,肿么回事。———小鱼的第一节课后作业
#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 % 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("¼ÆËãÖD...\n");
findAllCodes(path);
findAllDirs(path);
printf("Ä¿Ç°Äã×ü12D′áË %ld DD′úÂë!\n\n",total);
system("pause");
return 0;
}
ld returned 1 exit status (运行时提示的错误) 本帖最后由 jackz007 于 2020-12-15 12:45 编辑
你这个信息不是运行出错,而是代码没有能够通过连接。
编译通过了说明没有语法问题,可是连接出错( ld returned 1 exit status),原因是你代码中使用的某个外部变量或函数没有找到出处。你要把错误信息贴全,这样才会有解决问题的线索。 你的代码第 5 行 与第 10 行代码是乱码,复制我下面的代码,你再试试
// 有些编译器对缩进有严格的要求,所以,请不要改变每一行前的空格!
// 如果程序文件是 *.cpp,则将第 50 行的 "/*.c" 直接改为 "/*.cpp"
#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)// 如果程序文件是 *.cpp,将此行的 "/*.c" 直接改为 "/*.cpp"
{
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("目前您总共写了 %1d 行代码! \n\n", total);
system("pause");
return 0;
} 风过无痕1989 发表于 2020-12-15 12:58
你的代码第 5 行 与第 10 行代码是乱码,复制我下面的代码,你再试试
谢谢啊,你的我复制了可以正常运行。 jackz007 发表于 2020-12-15 12:40
你这个信息不是运行出错,而是代码没有能够通过连接。
编译通过了说明没有语法问题,可是连接 ...
啊啊啊,太高深了,刚开始学不太懂,不过谢谢你啊!
页:
[1]