鱼C论坛

 找回密码
 立即注册
查看: 6729|回复: 19

[已解决]昨天才开始学C语言,但做第一节课的课后习题,为什么一直运行不了

[复制链接]
发表于 2021-5-28 18:45:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

  1. #include <io.h>
  2. #include <direct.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. #define MAX        256

  7. long total;

  8. int countLines(const char *filename);
  9. void findAllCodes(const char *path);
  10. void findALLFiles(const char *path);

  11. int countLines(const char *filename)
  12. {
  13.         FILE *fp;
  14.         int count = 0;
  15.         int temp;
  16.         
  17.         if ((fp = fopen(filename, "r")) == NULL)
  18.         {
  19.                 fprintf(stderr, "Can not open the file:%s\n", filename);
  20.                 return 0;
  21.         }
  22.         
  23.         while ((temp = fgetc(fp)) != EOF)
  24.         {
  25.                 if (temp == '\n')
  26.                 {
  27.                         count++;
  28.                 }
  29.         }
  30.         
  31.         fclose(fp);
  32.         
  33.         return count;
  34. }

  35. void findAllCodes(const char *path)
  36. {
  37.         struct _finddata_t fa;
  38.         long handle;
  39.         char thePath[MAX], target[MAX];
  40.         
  41.         strcpy(thePath, path);
  42.         if((handle = _findfirst(strcat(thePath,"/*"), &fa)) == -1L)
  43.         {
  44.                 do
  45.                 {
  46.                         sprintf(target, "%s/%s", path, fa.name);
  47.                         total += countLines(target);
  48.                 }while (_findnext(handle, &fa) == 0);
  49.         }
  50.    
  51.         _findclose(handle);
  52. }

  53. void findALLDirs(const char *path)
  54. {
  55.         struct _finddata_t fa;
  56.         long handle;
  57.         char thePath[MAX];
  58.         
  59.         strcpy(thePath, path);
  60.         if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
  61.         {
  62.                 fprintf(stderr, "The path %s is wrong!\n",path);
  63.                 return;
  64.         }
  65.    
  66.         do
  67.         {        
  68.                 if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  69.                         continue;
  70.                     
  71.                 if( fa.attrib == _A_SUBDIR)
  72.                 {        
  73.                         sprintf(thePath, "%s/%s", path, fa.name);
  74.                         findAllCodes(thePath);
  75.                         findALLDirs(thePath);
  76.                 }
  77.         }while (_findnext(handle, &fa) == 0);
  78.    
  79.         _findclose(handle);   
  80. }

  81. int main()
  82. {
  83.         char path[MAX] = ".";
  84.         
  85.         printf("计算中...\n");
  86.         
  87.         findAllCodes(path);
  88.         findALLDirs(path);
  89.         
  90.         printf("目前你总共写了 %ld 行代码!\n\n", total);
  91.         system("pause");
  92.         
  93.         return 0;
  94. }
复制代码
最佳答案
2021-5-29 09:35:50
保存文件类型出错了,不是.cpp应该是.c
你用我这个代码,
  1. #include<io.h>
  2. #include<direct.h>
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<string.h>

  6. #define MAX                256

  7. long total;

  8. int countLines(const char *filename);
  9. void findALLCodes(const char *path);
  10. void findALLFiles(const char *path);

  11. int countLines(const char *filename)
  12. {
  13.         FILE *fp;
  14.         int count=0;
  15.         int temp;
  16.       
  17.         if((fp=fopen(filename,"r"))==NULL)
  18.         {
  19.                 fprintf(stderr,"Can not open the file:%s\n",filename);
  20.                 return 0;
  21.         }
  22.       
  23.         while((temp=fgetc(fp))!=EOF)
  24.         {
  25.                 if(temp=='\n')
  26.                 {
  27.                         count++;
  28.                 }
  29.         }
  30.       
  31.         fclose(fp);
  32.       
  33.         return count;
  34. }

  35. void findALLCodes(const char *path)
  36. {
  37.         struct _finddata_t fa;
  38.         long handle;
  39.         char thePath[MAX],target[MAX];
  40.       
  41.                 strcpy(thePath,path);
  42.                 if((handle=_findfirst(strcat(thePath,"/*.c"),&fa))!=-1L)
  43.                 {
  44.                         do
  45.                         {
  46.                                 sprintf(target,"%s%s",path,fa.name);
  47.                                 total+=countLines(target);
  48.                          }while(_findnext(handle,&fa)==0);
  49.                  }
  50.                  
  51.                  _findclose(handle);
  52. }

  53. void findALLDirs(const char *path)
  54. {
  55.         struct _finddata_t fa;
  56.         long handle;
  57.         char thePath[MAX];
  58.          
  59.          strcpy(thePath,path);
  60.          if((handle=_findfirst(strcat(thePath,"/*"),&fa))==-1L)
  61.          {
  62.                  fprintf(stderr,"The path%s is wrong!\n",path);
  63.                  return;
  64.           }
  65.          
  66.           do
  67.           {
  68.                   if(!strcmp(fa.name,".")||!strcmp(fa.name,".."))
  69.                    continue;
  70.                   
  71.                    if(fa.attrib==_A_SUBDIR)
  72.                    {
  73.                            sprintf(thePath,"%s%s",path,fa.name);
  74.                            findALLCodes(thePath);
  75.                            findALLDirs(thePath);
  76.                    }
  77.           }while(_findnext(handle,&fa)==0);
  78.          
  79.           _findclose(handle);
  80. }

  81. int main()
  82. {
  83.         char path[MAX]=".";
  84.       
  85.         printf("计算中...\n");
  86.       
  87.         findALLCodes(path);
  88.         findALLDirs(path);
  89.       
  90.         printf("目前你总共写了%d行代码!\n\n",total);
  91.         system("pause");
  92.       
  93.         return 0;
  94. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-28 20:22:37 | 显示全部楼层
上报错信息啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-28 20:34:04 | 显示全部楼层

file:///E:/1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-28 20:35:30 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-28 20:37:01 | 显示全部楼层

打字发出来还是图片啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-28 20:38:10 | 显示全部楼层
楚秋惜 发表于 2021-5-28 20:37
打字发出来还是图片啊

贴文字报错信息
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-28 20:38:14 | 显示全部楼层
E:\测试c\未命名1.cpp        In function 'void findAllCodes(const char*)':
49        51        E:\测试c\未命名1.cpp        [Error] 'fa' was not declared in this scope
E:\测试c\未命名1.cpp        In function 'void findALLDirs(const char*)':
63        21        E:\测试c\未命名1.cpp        [Error] aggregate 'findALLDirs(const char*)::_finddate_t fa' has incomplete type and cannot be defined
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-28 20:41:47 | 显示全部楼层

                E:\测试c\未命名1.cpp        In function 'void findAllCodes(const char*)':
49        51        E:\测试c\未命名1.cpp        [Error] 'fa' was not declared in this scope
                E:\测试c\未命名1.cpp        In function 'void findALLDirs(const char*)':
63        21        E:\测试c\未命名1.cpp        [Error] aggregate 'findALLDirs(const char*)::_finddate_t fa' has incomplete type and cannot be defined
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-28 22:42:00 | 显示全部楼层
第81行代码敲错了 findAllCodes(thePath);  应该是findALLCodes(thePath);,要和你上面写的函数保持一致
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-29 08:05:39 | 显示全部楼层
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        [Error] 'fa' was not declared in this scope
E:\测试c\未命名1.cpp        In function 'void findALLDirs(const char*)':
63        21        E:\测试c\未命名1.cpp        [Error] aggregate 'findALLDirs(const char*)::_finddate_t fa' has incomplete type and cannot be defined
82        24        E:\测试c\未命名1.cpp        [Error] 'findALLCodes' was not declared in this scope
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-29 09:35:50 | 显示全部楼层    本楼为最佳答案   
保存文件类型出错了,不是.cpp应该是.c
你用我这个代码,
  1. #include<io.h>
  2. #include<direct.h>
  3. #include<stdio.h>
  4. #include<stdlib.h>
  5. #include<string.h>

  6. #define MAX                256

  7. long total;

  8. int countLines(const char *filename);
  9. void findALLCodes(const char *path);
  10. void findALLFiles(const char *path);

  11. int countLines(const char *filename)
  12. {
  13.         FILE *fp;
  14.         int count=0;
  15.         int temp;
  16.       
  17.         if((fp=fopen(filename,"r"))==NULL)
  18.         {
  19.                 fprintf(stderr,"Can not open the file:%s\n",filename);
  20.                 return 0;
  21.         }
  22.       
  23.         while((temp=fgetc(fp))!=EOF)
  24.         {
  25.                 if(temp=='\n')
  26.                 {
  27.                         count++;
  28.                 }
  29.         }
  30.       
  31.         fclose(fp);
  32.       
  33.         return count;
  34. }

  35. void findALLCodes(const char *path)
  36. {
  37.         struct _finddata_t fa;
  38.         long handle;
  39.         char thePath[MAX],target[MAX];
  40.       
  41.                 strcpy(thePath,path);
  42.                 if((handle=_findfirst(strcat(thePath,"/*.c"),&fa))!=-1L)
  43.                 {
  44.                         do
  45.                         {
  46.                                 sprintf(target,"%s%s",path,fa.name);
  47.                                 total+=countLines(target);
  48.                          }while(_findnext(handle,&fa)==0);
  49.                  }
  50.                  
  51.                  _findclose(handle);
  52. }

  53. void findALLDirs(const char *path)
  54. {
  55.         struct _finddata_t fa;
  56.         long handle;
  57.         char thePath[MAX];
  58.          
  59.          strcpy(thePath,path);
  60.          if((handle=_findfirst(strcat(thePath,"/*"),&fa))==-1L)
  61.          {
  62.                  fprintf(stderr,"The path%s is wrong!\n",path);
  63.                  return;
  64.           }
  65.          
  66.           do
  67.           {
  68.                   if(!strcmp(fa.name,".")||!strcmp(fa.name,".."))
  69.                    continue;
  70.                   
  71.                    if(fa.attrib==_A_SUBDIR)
  72.                    {
  73.                            sprintf(thePath,"%s%s",path,fa.name);
  74.                            findALLCodes(thePath);
  75.                            findALLDirs(thePath);
  76.                    }
  77.           }while(_findnext(handle,&fa)==0);
  78.          
  79.           _findclose(handle);
  80. }

  81. int main()
  82. {
  83.         char path[MAX]=".";
  84.       
  85.         printf("计算中...\n");
  86.       
  87.         findALLCodes(path);
  88.         findALLDirs(path);
  89.       
  90.         printf("目前你总共写了%d行代码!\n\n",total);
  91.         system("pause");
  92.       
  93.         return 0;
  94. }
复制代码
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-29 12:07:38 | 显示全部楼层
zz学编程 发表于 2021-5-29 09:35
保存文件类型出错了,不是.cpp应该是.c
你用我这个代码,

我改了,还是不行

E:\测试c\4.c        In function 'findAllCodes':
48        52        E:\测试c\4.c        [Error] 'fa' undeclared (first use in this function)
48        52        E:\测试c\4.c        [Note] 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        [Error] storage size of 'fa' isn't known
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-29 14:25:00 | 显示全部楼层
这么离谱么,我都可以运行
1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-29 14:25:53 | 显示全部楼层
你用的啥编译器,我用的是devc++
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-30 08:32:50 | 显示全部楼层
zz学编程 发表于 2021-5-29 14:25
你用的啥编译器,我用的是devc++

我也是啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-30 13:43:50 | 显示全部楼层
48行打错了,自己复查一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-30 16:50:59 | 显示全部楼层
最强废铁h 发表于 2021-5-30 13:43
48行打错了,自己复查一下

我全部又对了一遍

E:\测试c\4.c        In function 'findAllCodes':
43        24        E:\测试c\4.c        [Error] storage size of 'fa' isn't known
E:\测试c\4.c        In function 'findALLDirs':
62        21        E:\测试c\4.c        [Error] storage size of 'fa' isn't known
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-30 16:52:26 | 显示全部楼层
  1. #include <io.h>
  2. #include <direct.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. #define MAX        256

  7. long total;

  8. int countLinex(const char *filename);
  9. void findALLCodes(const char *path);
  10. void findALLFiles(const char *path);

  11. int countLines(const char *filename)
  12. {
  13.     FILE *fp;
  14.         int count = 0;
  15.         int temp;
  16.        
  17.         if((fp = fopen(filename,"r")) == NULL)
  18.         {
  19.                 fprintf(stderr,"Can not open the film: %s\n",filename);
  20.                 return 0;
  21.         }
  22.        
  23.         while((temp = fgetc(fp)) != EOF)
  24.         {
  25.                 if (temp == '\n')
  26.                 {
  27.                         count++;
  28.                 }
  29.         }
  30.        
  31.         fclose(fp);
  32.        
  33.         return count;
  34.        
  35. }

  36. void findAllCodes(const char *path)
  37. {
  38.     struct _finddate_t fa;
  39.         long handle;
  40.         char thePath[MAX], target[MAX];
  41.        
  42.         strcpy(thePath, path);
  43.         if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) == -1L)
  44.         {
  45.                 do
  46.                 {
  47.                  sprintf(target,"%s/%s", path, fa.name);
  48.                  total += countLines(target);
  49.                 }while(_findnext(handle,&fa) == 0);
  50.         }
  51.        
  52.         _findclose(handle);
  53. }

  54. void findALLDirs(const char *path)
  55. {
  56.         struct _finddate_t fa;
  57.         long handle;
  58.         char thePath[MAX];
  59.        
  60.         strcpy(thePath, path);
  61.         if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
  62.         {
  63.                 fprintf(stderr,"The path %s is wrong!\n",path);
  64.                 return;
  65.         }
  66.        
  67.         do
  68.         {
  69.                 if(!strcmp(fa.name,".")|| !strcmp(fa.name, ".."))
  70.                 continue;
  71.                
  72.                 if( fa.attrib == _A_SUBDIR)
  73.                 {
  74.                         sprintf(thePath,"%s/%s", path, fa.name);
  75.                         findALLCodes(thePath);
  76.                         findALLDirs(thePath);
  77.                 }
  78.         }while (_findnext(handle, &fa) == 0);
  79.        
  80.         _findclose(handle);
  81. }

  82. int main()
  83. {
  84.         char path[MAX] = ".";
  85.        
  86.         printf("计算中...\n");
  87.        
  88.         findAllCodes(path);
  89.         findALLDirs(path);
  90.        
  91.         printf("目前你总共写了 %ld 行代码! \n\n", total);
  92.         system("pause");
  93.        
  94.         return 0;
  95. }
复制代码



E:\测试c\4.c        In function 'findAllCodes':
43        24        E:\测试c\4.c        [Error] storage size of 'fa' isn't known
E:\测试c\4.c        In function 'findALLDirs':
62        21        E:\测试c\4.c        [Error] storage size of 'fa' isn't known
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-30 21:29:03 | 显示全部楼层
楚秋惜 发表于 2021-5-30 16:52
E:\测试c\4.c        In function 'findAllCodes':
43        24        E:\测试c\4.c        [Erro ...

你还是照着重新敲一遍吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-9 18:24:31 | 显示全部楼层
计算中...
目前你总共写了0行代码!   是什么鬼  写完又检查完之后运行就这样子
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 12:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表