鱼C论坛

 找回密码
 立即注册
查看: 42|回复: 3

求助

[复制链接]
发表于 2026-9-5 19:21:42 | 显示全部楼层 |阅读模式

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

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

x

#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 findAllDirs(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;
}



为啥我的结果是错的?
只显示&d行
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2026-9-5 19:22:01 | 显示全部楼层
问题出在多处,最直接的是 printf 写错了。

先看这一句:


  1. printf("俩小时你个菜鸡就写了 &ld 行代码!\n\n", total);
复制代码


格式字符串要以 % 开头,不是 &。  
你写成了 &ld,所以 total 根本没被格式化输出,屏幕上就直接打印了 &ld 或类似的字符。

改成:


  1. printf("俩小时你个菜鸡就写了 %ld 行代码!\n\n", total);
复制代码


另外还有几个坑:

1. findAllCodes 里的 char thePath, target; 写错了。  
   这定义的是两个字符变量,不是字符数组。  
   strcpysprintf 会把字符串写进一个字符变量,导致内存越界。  
   应该改成:

  

  1.    char thePath[MAX];
  2.    char target[MAX];
  3.    
复制代码


2. findALLDirs 里的 char thePath; 同样要改成:

  

  1.    char thePath[MAX];
  2.    
复制代码


3. countlines 只统计 \n,如果文件的最后一行没有换行,那一行不会被统计进去。

4. 判断子目录时最好用位与:

  

  1.    if (fa.attrib & _A_SUBDIR)
  2.    
复制代码


   不要直接用 ==,因为可能有其他属性位同时存在。

先改掉大括号里的字符数组问题和 printf 的格式错误,结果应该就能正常显示了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2026-9-5 19:40:15 | 显示全部楼层
那我如何把自己打的代码放到这个目录里?,我用的是DEVc++小熊猫
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2026-9-5 19:40:34 | 显示全部楼层
peace知行 发表于 2026-09-05 19:40
那我如何把自己打的代码放到这个目录里?,我用的是DE ...


你的问题:如何让自己写的代码放进程序扫描的目录?

程序里的 path = "." 表示当前工作目录
当你用 DEV-C++(小熊猫版)运行时,当前工作目录通常是你的源代码文件所在文件夹,也可能是项目文件(.dev)所在文件夹

所以最简单的做法是:
  • 把你写好的 .c 源文件,和你这个统计程序放在同一个文件夹里。
  • 如果有很多文件,还可以在同一个文件夹里建子文件夹,再放进去,程序会自动递归扫描子目录。


如果你已经放了,但统计不到,那可能是当前工作目录和你放代码的目录不一致。这时候可以把路径改成完整的绝对路径,例如:


  1. char path[MAX] = "C:/Users/你的用户名/Desktop/mycode";
复制代码


就是把双引号里的路径换成你实际放代码的文件夹路径,注意要用 / 或者 \\ 分隔目录。

另外提醒你,原先代码里有两个明显的错误:
  • printf 里的格式符写错了,应该是 %ld,不是 &ld
  • char thePath, target; 声明成了单个字符,要改成字符数组,例如 char thePath[MAX];char target[MAX];


把代码和文件都放好后,再运行一次,就能正常统计了。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-9-18 19:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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