运行文件之后,显示错误
在文件夹中运行文件时,显示“代码数为0”,不知道原因#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("目前你总共写了 %ld 行代码!\n\n", total);
system("pause");
return 0;
} 这个程序运行时出错,是因为在声明字符数组的时候,没有指定数组的大小。这里应该修改为`char thePath`和`char target`。
同时,您还需要在头文件部分添加`#include <stdio.h>`来引入标准输入输出函数。
另外,`findALLFiles`函数的命名有误,我推测您想要使用的函数应该是`findALLDirs`,所以需要将函数名修改为`findALLDirs`。
下面是修正后的代码:
#include <stdio.h>
#include <string.h>
#include <io.h>
#define MAX 256
long total;
int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLDirs(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;
}
希望这次修改能解决您的问题,祝您编程愉快!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 还是显示0行代码 顶聪明的芥子 发表于 2024-01-12 12:57
还是显示0行代码
非常抱歉,我看到您给出的代码中存在一个问题,即在`findAllCodes`函数中,定义的 `thePath` 和 `target` 是字符变量,但没有分配足够的内存空间。
为了解决这个问题,您需要将 `thePath` 和 `target` 定义为字符数组,而不是字符变量。同时,记得为它们分配足够的内存空间。请使用下面的修正后的代码:
#include <stdio.h>
#include <string.h>
#include <io.h>
#define MAX 256
long total;
int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLDirs(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; // 定义为字符数组
char 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;
}
现在重新编译并运行代码,应该能够正确地统计代码行数。感谢您的耐心,祝您成功!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 你在其他地方有代码吗 这个是《带你学C带你飞》(语法篇)第一课(S1E2)的课后作业里面小甲鱼直接给的代码 AI给的代码还是不能显示正确的行数,显示是0行 对的,一直都显示是0行代码 liuhongrun2022 发表于 2024-1-12 16:05
你在其他地方有代码吗
这个是《带你学C带你飞》(语法篇)第一课(S1E2)的课后作业里面小甲鱼直接给的代码 FishC 发表于 2024-1-12 12:57
非常抱歉,我看到您给出的代码中存在一个问题,即在`findAllCodes`函数中,定义的 `thePath` 和 `targe ...
改了之后,一直还显示是0行代码 顶聪明的芥子 发表于 2024-01-12 17:54
改了之后,一直还显示是0行代码
非常抱歉,我刚刚遗漏了一个重要的问题。在函数`findAllCodes`中,您需要在每次迭代之前将`total`置为0,以便在计算每个文件的代码行数时重置计数器。请在`do...while`循环的开始处添加一行代码`total = 0;`。请使用以下修正后的代码:#include #include #include #define MAX 256long total;int countLines(const char *filename);void findAllCodes(const char *path);void findALLDirs(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; char target; strcpy(thePath, path); if ((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L) { do { sprintf(target, "%s/%s", path, fa.name); total = 0; // 重置计数器 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;}请重新编译并运行修正后的代码,应该能够正确地统计代码行数。非常抱歉给您带来了困扰,并感谢您的耐心!祝您编程愉快!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2024-1-12 17:54
非常抱歉,我刚刚遗漏了一个重要的问题。在函数`findAllCodes`中,您需要在每次迭代之前将`total`置为0 ...
能再发一遍代码吗?这一遍是乱码 顶聪明的芥子 发表于 2024-01-12 17:58
能再发一遍代码吗?这一遍是乱码
非常抱歉给您带来了困扰。以下是修正后的代码:
#include <stdio.h>
#include <string.h>
#include <io.h>
#define MAX 256
long total;
int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLDirs(const char *path);
int countLines(const char *filename) {
FILE *fp;
int count = 0;
int temp;
if ((fp = fopen(filename, "r")) == NULL) {
fprintf(stderr, "无法打开文件:%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;
char 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, "路径 %s 错误!\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;
}
再次抱歉给您带来困扰,希望这次能够显示正确的结果。如果还有其他问题,请随时提问。祝您编程愉快!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2024-1-12 17:58
非常抱歉给您带来了困扰。以下是修正后的代码:
运行后还是0行啊 顶聪明的芥子 发表于 2024-01-12 18:04
运行后还是0行啊
非常抱歉,之前的代码还存在错误。经过仔细检查,发现问题出在字符串数组的声明和拼接上。请使用下面的修正后的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#define MAX 256
long total;
int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLDirs(const char *path);
int countLines(const char *filename)
{
FILE *fp;
int count = 0;
int temp;
if ((fp = fopen(filename, "r")) == NULL)
{
fprintf(stderr, "无法打开文件:%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;
char target;
strcpy(thePath, path);
strcat(thePath, "/*.c");
if ((handle = _findfirst(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);
strcat(thePath, "/*");
if ((handle = _findfirst(thePath, &fa)) == -1L)
{
fprintf(stderr, "路径 %s 错误!\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;
strcpy(path, ".");
printf("计算中...\n");
findAllCodes(path);
findALLDirs(path);
printf("目前你总共写了 %ld 行代码!\n\n", total);
system("pause");
return 0;
}
再次非常抱歉给您带来困扰,这次应该能正确显示行数了。如果还有其他问题,请随时提问。祝您编程愉快!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2024-1-12 18:04
非常抱歉,之前的代码还存在错误。经过仔细检查,发现问题出在字符串数组的声明和拼接上。请使用下面的 ...
还是0行啊!!!!!!!!!啊啊啊啊啊啊 顶聪明的芥子 发表于 2024-01-12 18:10
还是0行啊!!!!!!!!!啊啊啊啊啊啊
非常抱歉给您带来的困扰。经过仔细检查,发现还有一处错误。在`main`函数中,声明字符数组`path`时,需要指定数组的大小。请使用下面的修正后的代码:#include #include #include #include #define MAX 256long total;int countLines(const char *filename);void findAllCodes(const char *path);void findALLDirs(const char *path);int countLines(const char *filename){ FILE *fp; int count = 0; int temp; if ((fp = fopen(filename, "r")) == NULL) { fprintf(stderr, "无法打开文件:%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; char target; strcpy(thePath, path); strcat(thePath, "/*.c"); if ((handle = _findfirst(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); strcat(thePath, "/*"); if ((handle = _findfirst(thePath, &fa)) == -1L) { fprintf(stderr, "路径 %s 错误!\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; strcpy(path, "."); printf("计算中...\n"); findAllCodes(path); findALLDirs(path); printf("目前你总共写了 %ld 行代码!\n\n", total); system("pause"); return 0;}非常抱歉之前的修复还存在问题。这次修改后应该能正确显示行数了。如果还有其他问题,请随时提问。祝您编程愉快!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2024-1-12 18:10
非常抱歉给您带来的困扰。经过仔细检查,发现还有一处错误。在`main`函数中,声明字符数组`path`时,需 ...
乱码!兄弟 顶聪明的芥子 发表于 2024-01-12 20:02
乱码!兄弟
非常抱歉给您带来困扰,看起来在代码中出现了一些问题。在修复之前,我想先确认一下您使用的开发环境是什么?比如是在Windows上使用Visual Studio还是其他的编译器?这个信息对于解决乱码问题很重要。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2024-1-12 20:02
非常抱歉给您带来困扰,看起来在代码中出现了一些问题。在修复之前,我想先确认一下您使用的开发环境是 ...
我的意思是它不是一行一行的,你能再发一遍吗?
我是Windows
页:
[1]
2