鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: MorganZZL

程序出了点问题,小白请求帮助

[复制链接]
 楼主| 发表于 2021-7-15 10:31:53 | 显示全部楼层
人造人 发表于 2021-7-14 23:55
没用过那个软件
找一找 报错信息,把报错信息贴出来
要贴完整,把编译器给出的所有提示都贴出来,代码 ...

#include "试一试.h"
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>

#define MAX 256

long total;

int countLines(const char *filename);
int isCode(const char *filename);
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;
}

int isCode(const char *filename)
{
        int length;

        length = (int)strlen(filename);
        
        if (!strcmp(filename + (length - 2), ".c"))
        {
                return 1;
        }
        else
        {
                return 0;
        }
}

void findAllDirs(const char *path)
{
        DIR *dp;
        struct dirent *entry;
        struct stat statbuf;

        if ((dp = opendir(path)) == NULL)
        {
                fprintf(stderr, "The path %s is wrong!\n", path);
                return;
        }

        chdir(path);
        while ((entry = readdir(dp)) != NULL)
        {
                lstat(entry->d_name, &statbuf);

                if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
                        continue;

                if (S_ISDIR(statbuf.st_mode))
                {
                        findAllDirs(entry->d_name);
                }
                else
                {
                        if (isCode(entry->d_name))
                        {
                                total += countLines(entry->d_name);
                        }
                }
        }

        chdir("..");
        closedir(dp);
}

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

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

        findAllDirs(path);

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

        return 0;
}



如果改成(int)strlen 没有任何报错信息,但是运作不了
如果不改的话,报错信息为:Implicit conversion loses integer precision: 'unsigned long' to 'int'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-15 10:52:06 | 显示全部楼层
MorganZZL 发表于 2021-7-15 10:31
#include "试一试.h"
#include
#include

首先
#include "试一试.h"
是什么鬼?

然后
这个代码在我这边没有问题
  1. $ cat main.c
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <dirent.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <sys/stat.h>

  8. #define MAX 256

  9. long total;

  10. int countLines(const char *filename);
  11. int isCode(const char *filename);
  12. void findAllDirs(const char *path);

  13. int countLines(const char *filename)
  14. {
  15.         FILE *fp;
  16.         int count = 0;
  17.         int temp;

  18.         if ((fp = fopen(filename, "r")) == NULL)
  19.         {
  20.                 fprintf(stderr, "Can not open the file: %s\n", filename);
  21.                 return 0;
  22.         }

  23.         while ((temp = fgetc(fp)) != EOF)
  24.         {
  25.                 if (temp == '\n')
  26.                 {
  27.                         count++;
  28.                 }
  29.         }

  30.         fclose(fp);

  31.         return count;
  32. }

  33. int isCode(const char *filename)
  34. {
  35.         int length;

  36.         length = (int)strlen(filename);

  37.         if (!strcmp(filename + (length - 2), ".c"))
  38.         {
  39.                 return 1;
  40.         }
  41.         else
  42.         {
  43.                 return 0;
  44.         }
  45. }

  46. void findAllDirs(const char *path)
  47. {
  48.         DIR *dp;
  49.         struct dirent *entry;
  50.         struct stat statbuf;

  51.         if ((dp = opendir(path)) == NULL)
  52.         {
  53.                 fprintf(stderr, "The path %s is wrong!\n", path);
  54.                 return;
  55.         }

  56.         chdir(path);
  57.         while ((entry = readdir(dp)) != NULL)
  58.         {
  59.                 lstat(entry->d_name, &statbuf);

  60.                 if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
  61.                         continue;

  62.                 if (S_ISDIR(statbuf.st_mode))
  63.                 {
  64.                         findAllDirs(entry->d_name);
  65.                 }
  66.                 else
  67.                 {
  68.                         if (isCode(entry->d_name))
  69.                         {
  70.                                 total += countLines(entry->d_name);
  71.                         }
  72.                 }
  73.         }

  74.         chdir("..");
  75.         closedir(dp);
  76. }

  77. int main()
  78. {
  79.         char path[MAX] = ".";

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

  81.         findAllDirs(path);

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

  83.         return 0;
  84. }
  85. $ gcc -g -Wall -o main main.c
  86. $ ./main
  87. 计算中...
  88. 目前你总共写了 487 行代码!

  89. $
复制代码


然后
编译器就给出了这一条提示?找一找你的那个软件,要编译器的完整输出


是你的编译环境问题,如果你不能给出完整的编译器输出,那这个问题我们没办法解决,就算是你给出了完整的编译器输出,这个问题也未必就能解决,因为我们对你现在用的那个环境不熟悉
也许你应该换一个大家都使用的编译环境
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-15 21:36:20 | 显示全部楼层
人造人 发表于 2021-7-15 10:52
首先
#include "试一试.h"
是什么鬼?

https://imgtu.com/i/WmMNkj
这个是错误提示的截屏,我也不清楚是不是,您可以看一下
然后我现在什么程序都执行不了了,我也不知道为什么。以前明明可以执行的现在也执行不了了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-18 14:14:47 | 显示全部楼层
MorganZZL 发表于 2021-7-15 21:36
https://imgtu.com/i/WmMNkj
这个是错误提示的截屏,我也不清楚是不是,您可以看一下
然后我现在什么程 ...

抱歉,我这边没有收到你的回复通知,现在才看到你的回复
报错提示是和 arm 相关的,你用的电脑是 arm 架构的?
还是换一个大家都使用的编译环境吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-5 12:56:06 | 显示全部楼层
看着报警说是:返回的是 unsigned long 类型,而你把它赋值给了int 型,都知道int 比long型小,转化过程中,超出int范围就存了个乱码进去了,所以报警了,最好就改成返回类型一致
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-23 21:42:39 | 显示全部楼层
如果有用的话,请设置最佳答案。
没问题的,可以运行

错误翻译:
隐式格式转换丢失精度:“unsigned long”到“int”

原因:
unsigned long 范围:0~ 4294967295 (即0到2^32-1)
int                 范围:-2147483648~2147483647(即-2^31~2^31-1)
建议:
黑体有改
int isCode(const char *filename)
{
    unsigned int length= strlen(filename);
    if (!strcmp(filename + (length - 2), ".c"))
    {
        return 1;
    }
    else
    {
        return 0;
    }
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-26 00:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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