鱼C论坛

 找回密码
 立即注册
查看: 2807|回复: 8

[学习笔记] 第一次上手敲(抄)脑溢血版

[复制链接]
发表于 2022-8-14 20:00:48 | 显示全部楼层 |阅读模式

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

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

x
[halemorning@morning morning]$ gcc count_lines.c -o count_lines
count_lines.c:16:1: 错误:程序中有游离的‘\357’
int countLines(const char *filename)
^
count_lines.c:16:1: 错误:程序中有游离的‘\274’
count_lines.c:16:1: 错误:程序中有游离的‘\210’
count_lines.c:16:18: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘const’
int countLines(const char *filename)
                  ^
count_lines.c:16:18: 错误:程序中有游离的‘\357’
count_lines.c:16:18: 错误:程序中有游离的‘\274’
count_lines.c:16:18: 错误:程序中有游离的‘\211’
count_lines.c: 在函数‘isCode’中:
count_lines.c:47:38: 警告:缺少结尾的 " 字符 [默认启用]
  if (!strcmp(filename + (length - 2),"c))
                                      ^
count_lines.c:47:2: 错误:缺少结尾的 " 字符
  if (!strcmp(filename + (length - 2),"c))
  ^
count_lines.c:48:2: 错误:expected expression before ‘{’ token
  {
  ^
count_lines.c:55:1: 错误:expected ‘)’ before ‘}’ token
}
^
count_lines.c:55:1: 错误:expected expression before ‘}’ token
count_lines.c: 在函数‘findAllDirs’中:
count_lines.c:83:29: 错误:expected ‘)’ before ‘;’ token
    if (isCode(entry->d_name);
                             ^
count_lines.c:87:3: 错误:expected expression before ‘}’ token
   }
   ^
count_lines.c: 在函数‘main’中:
count_lines.c:98:2: 错误:程序中有游离的‘\357’
  printf("计算中...\n");
  ^
count_lines.c:98:2: 错误:程序中有游离的‘\274’
count_lines.c:98:2: 错误:程序中有游离的‘\233’
count_lines.c:100:2: 错误:expected ‘;’ before ‘findAllDirs’
  findAllDirs(path);
  ^
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-8-14 20:08:48 | 显示全部楼层
#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;
        }

        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 pathp[MAX] = ".";

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

        findAllDirs(path);

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

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-14 20:35:30 | 显示全部楼层
本帖最后由 额外减小 于 2022-8-14 20:55 编辑
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <sys/stat.h>

  7. #define MAX 256

  8. long total;

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

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

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

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

  29.         fclose(fp);

  30.         return count;
  31. }

  32. int isCode(const char *filename)
  33. {
  34.         int length;
  35.       
  36.         length = 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 pathp[MAX] = ".";

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

  81.         findAllDirs(path);

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

  83.         return 0;
  84. }
复制代码

第16行 括号要用英文() 不能用这个()
第47行,你自己看看小甲鱼的代码就知道了,
  1.         if (!strcmp(filename + (length - 2), ".c"))
复制代码
,你的 "c 不是字符串,这个函数没法运行。
你把47行改对后,48行的"expected';'before '}' token"这个错误也就没了。
98行!!!你把分号;写成中文分号了;
96行 请问你定义的字符数组是pathp[MAX] 还是path[MAX]?
你先定义了pathp字符串,然后在后面又调用了不存在的path字符串

共勉
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-8-14 20:43:09 | 显示全部楼层
本帖最后由 人造人 于 2022-8-14 20:47 编辑
halemorning 发表于 2022-8-14 20:08
#include
#include
#include


要一个字符一个字符的检查,非常非常非常非常非非常非常认真的检查(这里着重强调认真,用了n个非常,用n个非常来强调认真,可见认真是多么的重要)
要一个字符也不能错,初学者因为不认真抄错很正常
认真一点,一字符一个字符的检查
count_lines.c:16:1: 错误:程序中有游离的‘\274’
学着看报错信息,这个16表示16行,去第16行附近看看吧
其他报错也是一样的,这个位置的数字是行号
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

 楼主| 发表于 2022-8-14 20:48:48 | 显示全部楼层
人造人 发表于 2022-8-14 20:43
要一个字符一个字符的检查,非常非常非常非常非非常非常认真的检查(这里着重强调认真,用了n个非常, ...

是的 打出来  正要一点一点查错去
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-14 20:50:33 | 显示全部楼层
halemorning 发表于 2022-8-14 20:48
是的 打出来  正要一点一点查错去

那你先查呀,自己先很认真的解决自己遇到的问题,实在解决不了再让别人帮忙
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-8-14 21:33:51 | 显示全部楼层
总结:
16行括号使用了中文括号
28行 !=  之间不能加空格?
47行   if (!strcmp(filename + (length - 2), ".c"))    后面按.c  按到是小键盘的.   和左面键盘的.  作用不一样
83行   if (isCode(entry->d_name))  抄成了 if (isCode(entry->d_name);
96行   char path[MAX] = ".";          抄成了 char pathp[MAX] = ".";
98行  使用了中文;

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
额外减小 + 1 + 1 天天向上

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-8-14 21:39:12 | 显示全部楼层
额外减小 发表于 2022-8-14 20:35
第16行 括号要用英文() 不能用这个()
第47行,你自己看看小甲鱼的代码就知道了,,你的 "c 不是字符串 ...

感谢指导!

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
额外减小 + 1 + 1 认真

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-8-14 21:39:47 | 显示全部楼层
人造人 发表于 2022-8-14 20:43
要一个字符一个字符的检查,非常非常非常非常非非常非常认真的检查(这里着重强调认真,用了n个非常, ...

感谢指导!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 19:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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