人群中最闪亮ID 发表于 2020-3-2 21:21:57

带你学C带你飞第一课作业的一个问题

动动手作业的程序写对了,运行也成功了,但是为什么显示的是一共写了0行代码,我保存的程序是要放到哪个文件夹里才能显示出来吗?求解{:10_266:}
还有怎么添加图片,我截图了但是放不了....

major_lyu 发表于 2020-3-2 21:26:29

代码中从哪个路径统计,就放在那啊!
最好把你的代码放上来

大河之jian 发表于 2020-3-2 22:05:44

我用的VC 6.0代码放在了My Projects文件里,这个是下载的时候就有的一个文件夹

major_lyu 发表于 2020-3-2 22:12:57

大河之jian 发表于 2020-3-2 22:05
我用的VC 6.0代码放在了My Projects文件里,这个是下载的时候就有的一个文件夹

代码发上来看看

人群中最闪亮ID 发表于 2020-3-3 08:07:05

major_lyu 发表于 2020-3-2 22:12
代码发上来看看

怎么发图

sanguine_boy 发表于 2020-3-3 09:41:01

人群中最闪亮ID 发表于 2020-3-3 08:07
怎么发图

直接发代码,不是发图

人群中最闪亮ID 发表于 2020-3-3 12:35:58

major_lyu 发表于 2020-3-2 22:12
代码发上来看看


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

#define MAX                256

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;
}

major_lyu 发表于 2020-3-3 13:10:27

本帖最后由 major_lyu 于 2020-3-3 13:17 编辑

人群中最闪亮ID 发表于 2020-3-3 12:35
#include
#include
#include


main函数中是从路径“./"开始查找的,是个相对路径,即从可执行文件的当前目录开始递归搜索每个子文件夹。所以你只要把要统计的代码放在编译后的可执行文件所在的文件夹,或者所在文件夹下的子文件夹里就可以了。

人群中最闪亮ID 发表于 2020-3-3 17:34:04

major_lyu 发表于 2020-3-3 13:10
main函数中是从路径“./"开始查找的,是个相对路径,即从可执行文件的当前目录开始递归搜索每个子文件 ...

还是0,我把所有编译保存的程序放在了一个文件夹里了(包括这个代码)

major_lyu 发表于 2020-3-3 19:46:08

你单步调试一下,看一看有没有读到你想要读的文件就行了

kiligsang 发表于 2021-2-20 21:18:46

major_lyu 发表于 2020-3-3 19:46
你单步调试一下,看一看有没有读到你想要读的文件就行了

单步调试什么意思。。我这一开始显示7行代码,后来不知道怎么了又显示0行了。我也是所有的代码都放在一个文件夹了

kiligsang 发表于 2021-2-20 21:20:06

kiligsang 发表于 2021-2-20 21:18
单步调试什么意思。。我这一开始显示7行代码,后来不知道怎么了又显示0行了。我也是所有的代码都放在一个 ...

好家伙!知道咋回事了,文件保存的格式没有选择c,选成了c++了
页: [1]
查看完整版本: 带你学C带你飞第一课作业的一个问题