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 = ".";

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

      findAllDirs(path);

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

      return 0;
}



如果改成(int)strlen 没有任何报错信息,但是运作不了
如果不改的话,报错信息为:Implicit conversion loses integer precision: 'unsigned long' to 'int'

人造人 发表于 2021-7-15 10:52:06

MorganZZL 发表于 2021-7-15 10:31
#include "试一试.h"
#include
#include


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

然后
这个代码在我这边没有问题
$ cat main.c
#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 = ".";

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

      findAllDirs(path);

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

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

$


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


是你的编译环境问题,如果你不能给出完整的编译器输出,那这个问题我们没办法解决,就算是你给出了完整的编译器输出,这个问题也未必就能解决,因为我们对你现在用的那个环境不熟悉
也许你应该换一个大家都使用的编译环境

MorganZZL 发表于 2021-7-15 21:36:20

人造人 发表于 2021-7-15 10:52
首先
#include "试一试.h"
是什么鬼?


https://imgtu.com/i/WmMNkj
这个是错误提示的截屏,我也不清楚是不是,您可以看一下
然后我现在什么程序都执行不了了,我也不知道为什么。以前明明可以执行的现在也执行不了了

人造人 发表于 2021-7-18 14:14:47

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

抱歉,我这边没有收到你的回复通知,现在才看到你的回复
报错提示是和 arm 相关的,你用的电脑是 arm 架构的?
还是换一个大家都使用的编译环境吧

lin53 发表于 2021-9-5 12:56:06

看着报警说是:返回的是 unsigned long 类型,而你把它赋值给了int 型,都知道int 比long型小,转化过程中,超出int范围就存了个乱码进去了,所以报警了,最好就改成返回类型一致

ExiaGN001 发表于 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;
    }
页: 1 [2]
查看完整版本: 程序出了点问题,小白请求帮助