lonly-Eagle 发表于 2020-7-23 23:51:30

为什么我照着写代码编译还出错。哪位大神帮看看。

本帖最后由 lonly-Eagle 于 2020-7-23 23:52 编辑

代码如下:
#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX256

long total;

int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLFiles(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;

}
void findAllCodes(const char *path)
{
        struct _finddata_t fa;
        long handle;
        char thePath,target;
       
        strcpy(thePath,path);
        if((handle = _findfirst(strcat(thePath,"/*.c"),&fa))! = -1L)
        {
                do
                {
                        sprintf(target,"%s/%s",path,fa.name);
                        total += countLines(target);
                }while (_findnext(handle,&fa)==0);
       
        }
       
        _findclose(handle);

}

void findALLDirs(const char *path)
{
      struct _finddata_t fa;
      long handle;
      char thePath;
      
      strcpy(thePath, path);
      if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
      {
                fprintf(stderr, "The path %s is wrong!\n",path);
                return;
      }
   
      do
      {      
                if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
                        continue;
                  
                if( fa.attrib == _A_SUBDIR)
                {      
                        sprintf(thePath, "%s/%s", path, fa.name);
                        findAllCodes(thePath);
                        findALLDirs(thePath);
                }
      }while (_findnext(handle, &fa) == 0);
   
      _findclose(handle);   
}

int main()
{
        char path=".";

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

        findAllCodes(path);
        findALLDirs(path);

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

        return 0;
}
注:用的是VS2010写代码

风过无痕1989 发表于 2020-7-24 00:55:55

这个问题已经有很多人问了,答案都是0行,我分别用VC++6.0 和DEV C++都试过了,都没得到101行

Twilight6 发表于 2020-7-24 07:44:09


48 行错了,你的 != 之间多加了空格 != ,正确代码:

#include <io.h>
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX256

long total;

int countLines(const char *filename);
void findAllCodes(const char *path);
void findALLFiles(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;

}
void findAllCodes(const char *path)
{
    struct _finddata_t fa;
    long handle;
    char thePath,target;

    strcpy(thePath,path);
    if((handle = _findfirst(strcat(thePath,"/*.c"),&fa)) != -1L)
    {
      do
      {
            sprintf(target,"%s/%s",path,fa.name);
            total += countLines(target);
      }while (_findnext(handle,&fa)==0);

    }

    _findclose(handle);

}

void findALLDirs(const char *path)
{
    struct _finddata_t fa;
    long handle;
    char thePath;

    strcpy(thePath, path);
    if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
    {
      fprintf(stderr, "The path %s is wrong!\n",path);
      return;
    }

    do
    {
      if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
            continue;

      if( fa.attrib == _A_SUBDIR)
      {
            sprintf(thePath, "%s/%s", path, fa.name);
            findAllCodes(thePath);
            findALLDirs(thePath);
      }
    }while (_findnext(handle, &fa) == 0);

    _findclose(handle);
}

int main()
{
    char path=".";

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

    findAllCodes(path);
    findALLDirs(path);

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

    return 0;
}

405794672 发表于 2020-7-24 08:10:22

你干脆直接复制人家的好了。

livcui 发表于 2020-7-24 08:29:32

这些函数都在main前面,怎么还要写原型{:10_277:}

sunrise085 发表于 2020-7-24 09:25:22

错误在48行
问题也在48行
48行的不等号 != 这是一个运算符,是一个整体,中间不能有空格。这是报错
运行不报错,若结果为0行,问题也出在这一行。若结果不是0行,请忽略后面这一句。你这里检测的是 .c 文件 ,你看看你在VS2010下写的是不是都是.cpp文件?

风过无痕1989 发表于 2020-7-24 11:16:06

sunrise085 发表于 2020-7-24 09:25
错误在48行
问题也在48行
48行的不等号 != 这是一个运算符,是一个整体,中间不能有空格。这是报错


楼主是写错了,我写的是 不等于 ,输出结果仍是 0

sunrise085 发表于 2020-7-24 11:18:21

风过无痕1989 发表于 2020-7-24 11:16
楼主是写错了,我写的是 不等于 ,输出结果仍是 0

检测文件类型哪里呢?
你写的程序文件是.cpp吧?这个程序检测的是.c文件

XYcarpenter 发表于 2020-7-24 11:20:17

发现抄错就重新再抄一遍,总有抄对的时候{:5_109:}

风过无痕1989 发表于 2020-7-24 11:24:23

sunrise085 发表于 2020-7-24 11:18
检测文件类型哪里呢?
你写的程序文件是.cpp吧?这个程序检测的是.c文件

是 C ,不是 CPP

风过无痕1989 发表于 2020-7-24 11:24:54

这是我的输出:

计算中. . .
Can not open the file:. 统计代码行数.c      // 冒号后有一个点
The path .Debug is wrongt !                     // Debug 前有一个点
目前你总共写了0 行代码:
请按任意键继续 . . .

风过无痕1989 发表于 2020-7-24 11:28:50

405794672 发表于 2020-7-24 08:10
你干脆直接复制人家的好了。

这应该是小甲鱼让我们练习写代码,复制就没有意义了

405794672 发表于 2020-7-25 08:43:17

风过无痕1989 发表于 2020-7-24 11:28
这应该是小甲鱼让我们练习写代码,复制就没有意义了

不懂的练习有屁用啊!在学习中,这么多代码够练习了。这个代码正好拿来用一下,只看一下。要研究,也得学完了才知道是怎么回事。

lonly-Eagle 发表于 2020-7-25 22:38:46

我编译运行后,对话框显示也是共写0行代码。而且程序后缀是.c,不是.cpp.
页: [1]
查看完整版本: 为什么我照着写代码编译还出错。哪位大神帮看看。