鱼C论坛

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

[已解决]Xiao JiaYu's C language class 1:code error, help!

[复制链接]
发表于 2018-1-27 21:20:48 | 显示全部楼层 |阅读模式

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

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

x
code
  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.        
  23.         while ((temp) = fgetc(fp) != EOF)
  24.         {
  25.             if (temp == '\n')
  26.             {
  27.                 count++;
  28.             }
  29.         }
  30.        
  31.         fclose(fp);
  32.        
  33.         return count;
  34. }

  35. int isCode(const char *filename)
  36. {
  37.     int length;
  38.    
  39.     length = strlen(filename);
  40.    
  41.     if (!strcamp(filename + (length-2), ".c"))
  42.     {
  43.         return 1;
  44.     }
  45.     else
  46.     {
  47.         return 0;
  48.     }
  49. }

  50. void findAllDirs(const char *path)
  51. {
  52.     DIR *dp;
  53.     struct dirent *entry;
  54.     struct stat statbuf;
  55.    
  56.     if ((dp = opendir(path)) == NULL)
  57.     {
  58.         fprintf(strerr, "The path %s is wrong!\n", path);
  59.         return;
  60.     }
  61.    
  62.     chdir(path);
  63.     while ((entry = reddir(dp)) != NULL)
  64.     {
  65.         lstat(entry->d_name, &stabuf);
  66.         
  67.         if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
  68.             continue;
  69.             
  70.         if (S_ISDIR(statbuf.st_mode))
  71.         {
  72.             findAllDirs(entry->d_name);
  73.         }
  74.         else
  75.         {
  76.             if (isCode(entry->d_name))
  77.             {
  78.                 total += countLines(entry->d_name);
  79.             }
  80.         }
  81.     }
  82.    
  83.     chdir("..");
  84.     closedir(dp);
  85. }

  86. int main()
  87. {
  88.     char path[MAX] = '.';
  89.    
  90.     printf("strat...\n");
  91.    
  92.     findAlllDirs(path);
  93.    
  94.     printf("%ld", total);
  95.    
  96.     return 0;
  97. }
复制代码


error
  1. sle1.c: In function ‘findAllDirs’:
  2. sle1.c:16:38: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
  3. int countLines(const char *filename) {
  4.                                       ^
  5. sle1.c:40:34: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
  6. int isCode(const char *filename) {
  7.                                   ^
  8. sle1.c:55:36: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
  9. void findAllDirs(const char *path) {
  10.                                     ^
  11. sle1.c:91:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
  12. int main() {
  13.             ^
  14. sle1.c:101:1: error: expected ‘{’ at end of input
  15. }
复制代码
最佳答案
2018-1-30 23:34:18
14 void findAllDirs(const char *path)  //少分号
22 if((fp = fopen(filename, 'r')) == NULL) // "r"
47 if (!strcamp(filename + (length-2), ".c")) //strcmp
65 fprintf(strerr, "The path %s is wrong!\n", path); //stderr
你可以看下别人的经验:
http://blog.163.com/njut_wangjian/blog/static/16579642520121022112940349/


抱歉,我没在linux环境下编过程,没办法帮你调试。最好对着小甲鱼老师的一行一行看
fighting!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-27 21:24:24 | 显示全部楼层
14行   ;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-28 15:09:07 | 显示全部楼层

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

使用道具 举报

发表于 2018-1-30 23:34:18 | 显示全部楼层    本楼为最佳答案   
14 void findAllDirs(const char *path)  //少分号
22 if((fp = fopen(filename, 'r')) == NULL) // "r"
47 if (!strcamp(filename + (length-2), ".c")) //strcmp
65 fprintf(strerr, "The path %s is wrong!\n", path); //stderr
你可以看下别人的经验:
http://blog.163.com/njut_wangjian/blog/static/16579642520121022112940349/


抱歉,我没在linux环境下编过程,没办法帮你调试。最好对着小甲鱼老师的一行一行看
fighting!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 21:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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