鱼C论坛

 找回密码
 立即注册
查看: 1123|回复: 6

[已解决]s1e2作业里有一行编码出了问题

[复制链接]
发表于 2020-9-6 13:32:13 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 大鱼szk 于 2020-9-6 13:32 编辑

关于下面代码中的这一行,if ((fp = fpoen(filename, "r")) == NULL)我打 了空格,用的是英语输入法中的等于号‘,运行时程序提示我等号前一个位置有问题,但我不知道问题出在了那里
      

  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 = fpoen(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.                
  36. void findAllCodes(const char *path)
  37. {
  38.             struct _finddata_t fa;
  39.             long handle;
  40.             char thePath[MAX], target[MAX];
  41.             
  42.             strcpy(thePath,path);
  43.             if((handle = _findfirst(stract(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 _finddata_t fa;
  57.             long handle;
  58.             char thePath[MAX];
  59.             
  60.             strcpy(thePath, path);
  61.             if((handle = _findfirdt(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.         _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. }
复制代码


最佳答案
2020-9-6 13:45:44
几个单词写错了
看注释
  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.         FILE *fp;
  13.         int count = 0;
  14.         int temp;

  15.         if ((fp = fopen(filename, "r")) == NULL) {// fopen
  16.                 fprintf(stderr, "Can not open the file: %s\n",filename);
  17.                 return 0;
  18.         }

  19.         while ((temp = fgetc(fp))!= EOF) {
  20.                 if(temp == '\n') {
  21.                         count++;
  22.                 }
  23.         }

  24.         fclose(fp);

  25.         return count;
  26. }

  27. void findAllCodes(const char *path) {
  28.         struct _finddata_t fa;
  29.         long handle;
  30.         char thePath[MAX], target[MAX];

  31.         strcpy(thePath,path);
  32.         if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L) {// strcat  
  33.                 do {
  34.                         sprintf(target, "%s/%s", path, fa.name);
  35.                         total += countLines(target);
  36.                 } while (_findnext(handle, &fa) == 0);
  37.         }

  38.         _findclose(handle);
  39. }

  40. void findALLDirs(const char *path) {
  41.         struct _finddata_t fa;
  42.         long handle;
  43.         char thePath[MAX];

  44.         strcpy(thePath, path);
  45.         if((handle =  _findfirst(strcat(thePath, "/*"),&fa)) == -1L) {//_findfirst
  46.                 fprintf(stderr, "The path %s is wrong!\n",path);
  47.                 return;
  48.         }

  49.         do {
  50.                 if(!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  51.                         continue;

  52.                 if( fa.attrib == _A_SUBDIR) {
  53.                         sprintf(thePath, "%s/%s", path, fa.name);
  54.                         findAllCodes(thePath);
  55.                         findALLDirs(thePath);
  56.                 }
  57.         } while (_findnext(handle, &fa) == 0);

  58.         _findclose(handle);
  59. }

  60. int main() {
  61.         char path[MAX] = ".";

  62.         printf("计算中...\n");

  63.         findAllCodes(path);// C要大写
  64.         findALLDirs(path);

  65.         printf("目前你总共写了 %ld 行代码! \n\n", total);
  66.         system("pause");

  67.         return 0;
  68. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-9-6 13:45:44 | 显示全部楼层    本楼为最佳答案   
几个单词写错了
看注释
  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.         FILE *fp;
  13.         int count = 0;
  14.         int temp;

  15.         if ((fp = fopen(filename, "r")) == NULL) {// fopen
  16.                 fprintf(stderr, "Can not open the file: %s\n",filename);
  17.                 return 0;
  18.         }

  19.         while ((temp = fgetc(fp))!= EOF) {
  20.                 if(temp == '\n') {
  21.                         count++;
  22.                 }
  23.         }

  24.         fclose(fp);

  25.         return count;
  26. }

  27. void findAllCodes(const char *path) {
  28.         struct _finddata_t fa;
  29.         long handle;
  30.         char thePath[MAX], target[MAX];

  31.         strcpy(thePath,path);
  32.         if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L) {// strcat  
  33.                 do {
  34.                         sprintf(target, "%s/%s", path, fa.name);
  35.                         total += countLines(target);
  36.                 } while (_findnext(handle, &fa) == 0);
  37.         }

  38.         _findclose(handle);
  39. }

  40. void findALLDirs(const char *path) {
  41.         struct _finddata_t fa;
  42.         long handle;
  43.         char thePath[MAX];

  44.         strcpy(thePath, path);
  45.         if((handle =  _findfirst(strcat(thePath, "/*"),&fa)) == -1L) {//_findfirst
  46.                 fprintf(stderr, "The path %s is wrong!\n",path);
  47.                 return;
  48.         }

  49.         do {
  50.                 if(!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  51.                         continue;

  52.                 if( fa.attrib == _A_SUBDIR) {
  53.                         sprintf(thePath, "%s/%s", path, fa.name);
  54.                         findAllCodes(thePath);
  55.                         findALLDirs(thePath);
  56.                 }
  57.         } while (_findnext(handle, &fa) == 0);

  58.         _findclose(handle);
  59. }

  60. int main() {
  61.         char path[MAX] = ".";

  62.         printf("计算中...\n");

  63.         findAllCodes(path);// C要大写
  64.         findALLDirs(path);

  65.         printf("目前你总共写了 %ld 行代码! \n\n", total);
  66.         system("pause");

  67.         return 0;
  68. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-9-6 16:31:10 | 显示全部楼层
楼上已正解,我回复得个荣誉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-8 20:23:24 | 显示全部楼层
baige 发表于 2020-9-6 13:45
几个单词写错了
看注释

不好意思,注释是运行时下面的错误吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-8 22:20:57 | 显示全部楼层
大鱼szk 发表于 2020-9-8 20:23
不好意思,注释是运行时下面的错误吗

他的注释就是告诉你,你错在哪儿了

比如:第82行 findAllCodes(path);   // C要大写
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-8 22:29:38 | 显示全部楼层
风过无痕1989 发表于 2020-9-8 22:20
他的注释就是告诉你,你错在哪儿了

比如:第82行 findAllCodes(path);   // C要大写

偶,晓得了,不过要是Decv语言选择的中文,那他的注释是不是也是中文的?我当时选语言选的是英语
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-8 23:08:17 | 显示全部楼层
大鱼szk 发表于 2020-9-8 22:29
偶,晓得了,不过要是Decv语言选择的中文,那他的注释是不是也是中文的?我当时选语言选的是英语{:10_247: ...

这个应该跟你选择使用什么语言没有关系吧?即便是英文版,作为字符显示的中文,应该是可以显示的。你可以试试,我的是中文版,没法试这个。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 18:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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