鱼C论坛

 找回密码
 立即注册
查看: 1447|回复: 10

[已解决]新人求问第二节课动动手

[复制链接]
发表于 2020-11-11 09:20:27 | 显示全部楼层 |阅读模式
4鱼币


我认真对着小甲鱼给的代码敲了,开始总是报错,我检查修改了几遍后,终于不报错了,可是程序却不能实现计数的功能:

图片:



我写的代码:


#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[MAX], target[MAX];
               
                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[MAX];
                 
                 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[MAX] =".";
                       
                  printf ("计算中... \n");
                       
                  findAllCodes (path);
                  findALLDirs (path);
                  
                  printf("目前你总共写了 %1d 行代码!\n\n",total);
                  system("pause") ;
                  
                  return 0;
  }

求大佬指点,作为回报,我将献出我微不足道的鱼币!
最佳答案
2020-11-11 09:20:28
看了一下就是你这个缩进有一点问题,帮你稍微改了一改,还有记得在电脑上让这个文件后缀名是.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("目前你总共写了 %ld 行代码!\n\n", total);
  91.         system("pause");
  92.         
  93.         return 0;
  94. }
复制代码

最佳答案

查看完整内容

看了一下就是你这个缩进有一点问题,帮你稍微改了一改,还有记得在电脑上让这个文件后缀名是.c
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-11 09:20:28 | 显示全部楼层    本楼为最佳答案   
看了一下就是你这个缩进有一点问题,帮你稍微改了一改,还有记得在电脑上让这个文件后缀名是.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("目前你总共写了 %ld 行代码!\n\n", total);
  91.         system("pause");
  92.         
  93.         return 0;
  94. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-11 09:52:56 | 显示全部楼层
复制你的代码  

void findAllCodes(const char * path)
    ....

          if((handle = _findfirst(strcat(thePath, "*.c") , &fa)) != -1L)   //  用的 英文 全角,改成用 英文半角 输入就可以了,


void findALLDirs(const char *path)
...

                if((handle = _findfirst(strcat(thePath,"*"), &fa)) == -1L)//同上


我改完之后就输出正常 了

Snipaste_2020-11-11_09-51-44.bmp
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-11 11:22:37 | 显示全部楼层
将文件夹下的. cpp后缀改为.c 即可
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-11-11 19:54:32 | 显示全部楼层

看过各位大佬宝贵的建议后,我又重写了一遍count_line,结果出现代码行数虚高的现象:

代码行数虚高

代码行数虚高

我附了一张图(不知道附成功了没,因为我只看到了一串代码emmmmm)
图片上显示编译执行后我的FishC里有6119行代码,可实际上我的代码只有333行
琢磨了半天,实在发现不了,只能求助大佬们了

我的代码是这样的:

#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[MAX],target[MAX];
       
        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[MAX];
       
        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[MAX] =".";
       
        printf("计算中...\n");
       
        findAllCodes(path);
        findALLDirs(path);
       
        printf("目前你总共写了 %d 行代码!\n\n",total);
        system("pause");
       
        return 0;
}

大佬们有发现什么吗?

嗯嗯,要是我的帖中没有附带图片还请大佬们教我一下如何附上图片;有的话就通知我一下,我是有附上图片的

蟹蟹啦,嘿!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-11-11 19:56:18 | 显示全部楼层
LuLD 发表于 2020-11-11 09:52
复制你的代码  

void findAllCodes(const char * path)

嘿,大佬你这图片是咋搞出来的,是贴图吗
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-11-11 19:58:28 | 显示全部楼层
LuLD 发表于 2020-11-11 09:52
复制你的代码  

void findAllCodes(const char * path)

还有联机搜索那是啥玩意,感觉挺好使的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-11 20:11:55 | 显示全部楼层
似花颜 发表于 2020-11-11 19:56
嘿,大佬你这图片是咋搞出来的,是贴图吗

嗯,贴图

Snipaste_2020-11-11_09-51-44.bmp
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-11 20:14:37 | 显示全部楼层
似花颜 发表于 2020-11-11 19:58
还有联机搜索那是啥玩意,感觉挺好使的

我用的 是 VS2019 版本的,鼠标放在写的函数啊,变量什么上面就会有出来解释的,联机搜索就是在网络上搜索这个函数的解释的网页之类的,就跟在百度里搜索问题答案 的 意思
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-12 14:49:24 | 显示全部楼层
似花颜 发表于 2020-11-11 19:54
看过各位大佬宝贵的建议后,我又重写了一遍count_line,结果出现代码行数虚高的现象:

我附了一张图( ...

        while((temp = fgetc(fp))!= EOF)
        {
                if(temp = '\n') // if(判断条件 ) if(temp == '\n'),你这样写temp = '\n' 成 赋值了
                {
                        count++;
                }
        }


代码 添加

Snipaste_2020-11-12_14-48-53.bmp

打开后将代码贴进去
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-12 20:28:52 | 显示全部楼层
我不要你的鱼币,只要给个最佳答案就行。出去旅游多日,我已经满后很多了

  1. // 有些编译器对缩进有严格的要求,所以,请不要改变每一行前的空格!
  2. // 如果程序文件是 *.cpp,则将第 50 行的 "/*.c" 直接改为 "/*.cpp"

  3. #include <io.h>
  4. #include <direct.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>

  8. #define MAX        256

  9. long total;

  10. int countlines(const char *filename);
  11. void findAllCodes(const char *path);
  12. void findALLFiles(const char *path);

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

  37. void findAllCodes(const char *path)
  38. {
  39.         struct _finddata_t fa;
  40.         long handle;
  41.         char thePath[MAX], target[MAX];
  42.         
  43.         strcpy(thePath, path);
  44.         if((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)  // 如果程序文件是 *.cpp,将此行的 "/*.c" 直接改为 "/*.cpp"
  45.         {
  46.                 do
  47.                 {
  48.                         sprintf(target, "%s/%s", path, fa.name);
  49.                         total += countlines(target);
  50.                 }while (_findnext(handle, &fa) == 0);
  51.         }
  52.    
  53.         _findclose(handle);
  54. }

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

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-9 19:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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