鱼C论坛

 找回密码
 立即注册
查看: 1717|回复: 5

[已解决]有看不懂的报错,求救

[复制链接]
发表于 2021-5-16 20:20:21 | 显示全部楼层 |阅读模式

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

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

x
s1e2课中计算代码的程序出现情况 屏幕截图 2021-05-16 200119.png 看不太懂,应该怎么改
我的:
#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 countLine(const char *filename);
int isCode(const char *filename);
void findAllDir(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 = strlen(filename);

        if (!istrcmp(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))
                {
                        findeAllDirs(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;
}


小甲鱼是这样的:
#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 = 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;
        }


最佳答案
2021-5-16 20:59:26
本帖最后由 肖-肖 于 2021-5-16 21:01 编辑

我把你代码中得错误找出来了,在下边,报错信息也提示你函数没有定义准确所以会找不到

  1. int countLine(const char *filename);
  2. //应该是这样--》int countLines(const char *filename);
  3. int isCode(const char *filename);
  4. void findAllDir(const char *path);
  5. //应该是这样--》void findAllDirs(const char *path);

  6. int isCode(const char *filename)
  7. {
  8.         int length;

  9.         length = strlen(filename);
  10.         //下边这里你写错了:你写的是: if (!istrcmp(filename + (length - 2),"c"))
  11.         if (!strcmp(filename + (length - 2), ".c"))
  12.         {
  13.                 return 1;
  14.         }
  15.         else
  16.         {
  17.                 return 0;
  18.         }
  19. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-16 20:55:37 | 显示全部楼层
代码有些地方,抄错了 字母了 ,我看了一下有很多地方都抄错了,  至于错在哪里我就不告诉你了,  你自己 把代码拆开一段一段的检查,相信你会看出来的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-5-16 20:59:26 | 显示全部楼层    本楼为最佳答案   
本帖最后由 肖-肖 于 2021-5-16 21:01 编辑

我把你代码中得错误找出来了,在下边,报错信息也提示你函数没有定义准确所以会找不到

  1. int countLine(const char *filename);
  2. //应该是这样--》int countLines(const char *filename);
  3. int isCode(const char *filename);
  4. void findAllDir(const char *path);
  5. //应该是这样--》void findAllDirs(const char *path);

  6. int isCode(const char *filename)
  7. {
  8.         int length;

  9.         length = strlen(filename);
  10.         //下边这里你写错了:你写的是: if (!istrcmp(filename + (length - 2),"c"))
  11.         if (!strcmp(filename + (length - 2), ".c"))
  12.         {
  13.                 return 1;
  14.         }
  15.         else
  16.         {
  17.                 return 0;
  18.         }
  19. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-18 19:24:10 | 显示全部楼层
做最好的自己520 发表于 2021-5-16 20:55
代码有些地方,抄错了 字母了 ,我看了一下有很多地方都抄错了,  至于错在哪里我就不告诉你了,  你自己  ...

我看到了,实在是太粗心了,汗颜
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-5-18 19:25:55 | 显示全部楼层
肖-肖 发表于 2021-5-16 20:59
我把你代码中得错误找出来了,在下边,报错信息也提示你函数没有定义准确所以会找不到

谢谢,很有用,程序已经改过来了,终于知道了这个报错的意思,有收获
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-5-18 20:09:43 | 显示全部楼层
向舒 发表于 2021-5-18 19:25
谢谢,很有用,程序已经改过来了,终于知道了这个报错的意思,有收获

嗯 问题解决了的话 设置下最佳答案吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 09:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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