鱼C论坛

 找回密码
 立即注册
查看: 1991|回复: 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" 文件,并显示每一个被统计文件的路径和行数。
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX        256

int countLines(const char * filename)
{
        char ch                                                                          ;
        FILE * fp                                                                        ;
        int c                                                                            ;
        if((fp = fopen(filename , "r"))) {
                for(c = 0 ; (ch = fgetc(fp)) != EOF ;) if(ch == '\n') c ++               ;  
                fclose(fp)                                                               ;
        }  else {
                fprintf(stderr , "Can not open the file : %s\n" , filename)              ;
        } 
        printf("[%3d] - %s\n" , c , filename)                                            ;     
        return c                                                                         ;
}

int findALLDirs(const char * path)
{
        struct _finddata_t fa                                                            ;
        long handle                                                                      ;
        char thePath[MAX]                                                                ;
        int i , k , n , r = 0                                                            ;
        strcpy(thePath , path)                                                           ;
        if((handle = _findfirst(strcat(thePath , "/*") , & fa)) != EOF) {
                do {
                        if(strcmp(fa . name , ".") && strcmp(fa . name , "..")) {
                                sprintf(thePath , "%s/%s" , path , fa . name)            ;
                                if(fa . attrib == _A_SUBDIR) {
                                        r += findALLDirs(thePath)                        ;
                                } else {
                                        for(n = 0 ; fa . name[n] ; n ++)                 ;
                                        for(k = n ; k && fa . name[k - 1] != '.' ; k --) ;
                                        if(fa . name[k - 1] == '.') {
                                                if(! stricmp(& fa . name[k - 1] , ".c") || ! stricmp(& fa . name[k - 1] , ".h") || ! stricmp(& fa . name[k - 1] , ".cpp")) {
                                                        r += countLines(thePath)         ;        
                                                }
                                        }            
                                }
                        }
                } while(! _findnext(handle , & fa))                                      ;
                _findclose(handle)                                                       ;   
        } else {
                fprintf(stderr, "The path %s is wrong!\n" , path)                        ;
        }
        return r                                                                         ;
}

int main(void)
{
        char path[MAX] = "."                                                             ;
        printf("计算中...\n")                                                            ;
        printf("目前你总共写了 %d 行代码!\n\n" , findALLDirs(path))                     ;
        system("pause")                                                                  ;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-9-29 00:01:38 | 显示全部楼层
我用的DveC
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

使用道具 举报

 楼主| 发表于 2022-9-29 08:09:53 | 显示全部楼层
D:\桌面QQ\截图20220928235608
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

大佬我不会发图片,而且我是第二天学,确实是不知道为什么会这样
想知道小甲鱼最近在做啥?请访问 -> 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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

代码就是这个样子,我怕抄错了又复制了一次还是0行,源文件路径是D:\C\summary
想知道小甲鱼最近在做啥?请访问 -> 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" 文件,并显示每一个被统计文件的路径和行数。
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX        256

int countLines(const char * filename)
{
        char ch                                                                          ;
        FILE * fp                                                                        ;
        int c                                                                            ;
        if((fp = fopen(filename , "r"))) {
                for(c = 0 ; (ch = fgetc(fp)) != EOF ;) if(ch == '\n') c ++               ;  
                fclose(fp)                                                               ;
        }  else {
                fprintf(stderr , "Can not open the file : %s\n" , filename)              ;
        } 
        printf("[%3d] - %s\n" , c , filename)                                            ;     
        return c                                                                         ;
}

int findALLDirs(const char * path)
{
        struct _finddata_t fa                                                            ;
        long handle                                                                      ;
        char thePath[MAX]                                                                ;
        int i , k , n , r = 0                                                            ;
        strcpy(thePath , path)                                                           ;
        if((handle = _findfirst(strcat(thePath , "/*") , & fa)) != EOF) {
                do {
                        if(strcmp(fa . name , ".") && strcmp(fa . name , "..")) {
                                sprintf(thePath , "%s/%s" , path , fa . name)            ;
                                if(fa . attrib == _A_SUBDIR) {
                                        r += findALLDirs(thePath)                        ;
                                } else {
                                        for(n = 0 ; fa . name[n] ; n ++)                 ;
                                        for(k = n ; k && fa . name[k - 1] != '.' ; k --) ;
                                        if(fa . name[k - 1] == '.') {
                                                if(! stricmp(& fa . name[k - 1] , ".c") || ! stricmp(& fa . name[k - 1] , ".h") || ! stricmp(& fa . name[k - 1] , ".cpp")) {
                                                        r += countLines(thePath)         ;        
                                                }
                                        }            
                                }
                        }
                } while(! _findnext(handle , & fa))                                      ;
                _findclose(handle)                                                       ;   
        } else {
                fprintf(stderr, "The path %s is wrong!\n" , path)                        ;
        }
        return r                                                                         ;
}

int main(void)
{
        char path[MAX] = "."                                                             ;
        printf("计算中...\n")                                                            ;
        printf("目前你总共写了 %d 行代码!\n\n" , findALLDirs(path))                     ;
        system("pause")                                                                  ;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

感谢大佬感谢大佬,问题解决了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-17 19:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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