鱼C论坛

 找回密码
 立即注册
查看: 2852|回复: 8

[已解决]寻求大佬帮助

[复制链接]
发表于 2022-9-28 23:59:55 | 显示全部楼层 |阅读模式

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

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

x

c语言第一节的课后作业的代码行数统计我一直显示零行,有办法解决吗?win11系统,文件夹里有其他源文件 谢谢大佬们了
最佳答案
2022-9-29 09:36:47
本帖最后由 jackz007 于 2022-9-29 10:12 编辑
高肆玖捌伍 发表于 2022-9-29 08:24
代码就是这个样子,我怕抄错了又复制了一次还是0行,源文件路径是D:\C\summary


         这个代码是正确的,这个代码统计的是当前程序所在目录及其各级子目录内所有文件名可以匹配 "*.c" 的文件。
         试试我修改的版本吧,我修改的这个版本除了统计 ".c" 外,还统计 ".h"、".cpp" 文件,并显示每一个被统计文件的路径和行数。
  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. int countLines(const char * filename)
  8. {
  9.         char ch                                                                          ;
  10.         FILE * fp                                                                        ;
  11.         int c                                                                            ;
  12.         if((fp = fopen(filename , "r"))) {
  13.                 for(c = 0 ; (ch = fgetc(fp)) != EOF ;) if(ch == '\n') c ++               ;  
  14.                 fclose(fp)                                                               ;
  15.         }  else {
  16.                 fprintf(stderr , "Can not open the file : %s\n" , filename)              ;
  17.         }
  18.         printf("[%3d] - %s\n" , c , filename)                                            ;     
  19.         return c                                                                         ;
  20. }

  21. int findALLDirs(const char * path)
  22. {
  23.         struct _finddata_t fa                                                            ;
  24.         long handle                                                                      ;
  25.         char thePath[MAX]                                                                ;
  26.         int i , k , n , r = 0                                                            ;
  27.         strcpy(thePath , path)                                                           ;
  28.         if((handle = _findfirst(strcat(thePath , "/*") , & fa)) != EOF) {
  29.                 do {
  30.                         if(strcmp(fa . name , ".") && strcmp(fa . name , "..")) {
  31.                                 sprintf(thePath , "%s/%s" , path , fa . name)            ;
  32.                                 if(fa . attrib == _A_SUBDIR) {
  33.                                         r += findALLDirs(thePath)                        ;
  34.                                 } else {
  35.                                         for(n = 0 ; fa . name[n] ; n ++)                 ;
  36.                                         for(k = n ; k && fa . name[k - 1] != '.' ; k --) ;
  37.                                         if(fa . name[k - 1] == '.') {
  38.                                                 if(! stricmp(& fa . name[k - 1] , ".c") || ! stricmp(& fa . name[k - 1] , ".h") || ! stricmp(& fa . name[k - 1] , ".cpp")) {
  39.                                                         r += countLines(thePath)         ;        
  40.                                                 }
  41.                                         }            
  42.                                 }
  43.                         }
  44.                 } while(! _findnext(handle , & fa))                                      ;
  45.                 _findclose(handle)                                                       ;   
  46.         } else {
  47.                 fprintf(stderr, "The path %s is wrong!\n" , path)                        ;
  48.         }
  49.         return r                                                                         ;
  50. }

  51. int main(void)
  52. {
  53.         char path[MAX] = "."                                                             ;
  54.         printf("计算中...\n")                                                            ;
  55.         printf("目前你总共写了 %d 行代码!\n\n" , findALLDirs(path))                     ;
  56.         system("pause")                                                                  ;
  57. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-9-29 00:01:38 | 显示全部楼层
我用的DveC
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-29 00:18:08 From FishC Mobile | 显示全部楼层
本帖最后由 jackz007 于 2022-9-29 00:19 编辑

       解决问题就要把有问题的东西展示出来啊。你感觉 0 行代码的问题出在哪里?不会认为自己真的就写了 0 行代码吧?既然是写了,为什么会 0 行,你告诉我,问题会出在哪里?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-29 08:09:53 | 显示全部楼层
D:\桌面QQ\截图20220928235608
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-29 08:11:53 | 显示全部楼层
jackz007 发表于 2022-9-29 00:18
解决问题就要把有问题的东西展示出来啊。你感觉 0 行代码的问题出在哪里?不会认为自己真的就写了 0 ...

大佬我不会发图片,而且我是第二天学,确实是不知道为什么会这样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-29 08:18:30 | 显示全部楼层

#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("目前你总共写了 %ld 行代码!\n\n", total);
        system("pause");
        
        return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-29 08:24:26 | 显示全部楼层
高肆玖捌伍 发表于 2022-9-29 08:18
#include
#include
#include

代码就是这个样子,我怕抄错了又复制了一次还是0行,源文件路径是D:\C\summary
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-9-29 09:36:47 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-9-29 10:12 编辑
高肆玖捌伍 发表于 2022-9-29 08:24
代码就是这个样子,我怕抄错了又复制了一次还是0行,源文件路径是D:\C\summary


         这个代码是正确的,这个代码统计的是当前程序所在目录及其各级子目录内所有文件名可以匹配 "*.c" 的文件。
         试试我修改的版本吧,我修改的这个版本除了统计 ".c" 外,还统计 ".h"、".cpp" 文件,并显示每一个被统计文件的路径和行数。
  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. int countLines(const char * filename)
  8. {
  9.         char ch                                                                          ;
  10.         FILE * fp                                                                        ;
  11.         int c                                                                            ;
  12.         if((fp = fopen(filename , "r"))) {
  13.                 for(c = 0 ; (ch = fgetc(fp)) != EOF ;) if(ch == '\n') c ++               ;  
  14.                 fclose(fp)                                                               ;
  15.         }  else {
  16.                 fprintf(stderr , "Can not open the file : %s\n" , filename)              ;
  17.         }
  18.         printf("[%3d] - %s\n" , c , filename)                                            ;     
  19.         return c                                                                         ;
  20. }

  21. int findALLDirs(const char * path)
  22. {
  23.         struct _finddata_t fa                                                            ;
  24.         long handle                                                                      ;
  25.         char thePath[MAX]                                                                ;
  26.         int i , k , n , r = 0                                                            ;
  27.         strcpy(thePath , path)                                                           ;
  28.         if((handle = _findfirst(strcat(thePath , "/*") , & fa)) != EOF) {
  29.                 do {
  30.                         if(strcmp(fa . name , ".") && strcmp(fa . name , "..")) {
  31.                                 sprintf(thePath , "%s/%s" , path , fa . name)            ;
  32.                                 if(fa . attrib == _A_SUBDIR) {
  33.                                         r += findALLDirs(thePath)                        ;
  34.                                 } else {
  35.                                         for(n = 0 ; fa . name[n] ; n ++)                 ;
  36.                                         for(k = n ; k && fa . name[k - 1] != '.' ; k --) ;
  37.                                         if(fa . name[k - 1] == '.') {
  38.                                                 if(! stricmp(& fa . name[k - 1] , ".c") || ! stricmp(& fa . name[k - 1] , ".h") || ! stricmp(& fa . name[k - 1] , ".cpp")) {
  39.                                                         r += countLines(thePath)         ;        
  40.                                                 }
  41.                                         }            
  42.                                 }
  43.                         }
  44.                 } while(! _findnext(handle , & fa))                                      ;
  45.                 _findclose(handle)                                                       ;   
  46.         } else {
  47.                 fprintf(stderr, "The path %s is wrong!\n" , path)                        ;
  48.         }
  49.         return r                                                                         ;
  50. }

  51. int main(void)
  52. {
  53.         char path[MAX] = "."                                                             ;
  54.         printf("计算中...\n")                                                            ;
  55.         printf("目前你总共写了 %d 行代码!\n\n" , findALLDirs(path))                     ;
  56.         system("pause")                                                                  ;
  57. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-9-29 19:09:56 | 显示全部楼层
jackz007 发表于 2022-9-29 09:36
这个代码是正确的,这个代码统计的是当前程序所在目录及其各级子目录内所有文件名可以匹配 "* ...

感谢大佬感谢大佬,问题解决了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-17 07:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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