鱼C论坛

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

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

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

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

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

x
code
#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 (!strcamp(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(strerr, "The path %s is wrong!\n", path);
        return;
    }
    
    chdir(path);
    while ((entry = reddir(dp)) != NULL)
    {
        lstat(entry->d_name, &stabuf);
        
        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("strat...\n");
    
    findAlllDirs(path);
    
    printf("%ld", total);
    
    return 0;
}

error
sle1.c: In function ‘findAllDirs’:
sle1.c:16:38: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
 int countLines(const char *filename) {
                                      ^
sle1.c:40:34: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
 int isCode(const char *filename) {
                                  ^
sle1.c:55:36: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
 void findAllDirs(const char *path) {
                                    ^
sle1.c:91:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
 int main() {
            ^
sle1.c:101:1: error: expected ‘{’ at end of input
 }
最佳答案
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-10-1 09:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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