鱼C论坛

 找回密码
 立即注册
查看: 1440|回复: 5

[已解决]小白求助!!第一课课后作业。

[复制链接]
发表于 2020-3-5 11:27:09 | 显示全部楼层 |阅读模式

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

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

x
全部代码敲完了,为什么会出现  “E:\MyCppWorkItem\collect2.exe        [Error] ld returned 1 exit status”  这个错误提示呢?
最佳答案
2020-3-5 12:31:07
1522843196 发表于 2020-3-5 12:06
#include
#include
#include
  1. #include <io.h>
  2. #include <direct.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. #define MAX       256

  7. long total;

  8. int countLines(const char *filename);
  9. void findAllCodes(const char *path);
  10. void findALLfiles(const char *path);
  11. void findALLdirs(const char *path);

  12. int countLines(const char*filename)
  13. {
  14.               FILE *fp;
  15.               int count = 0;
  16.               int temp;
  17.               
  18.               if((fp = fopen(filename, "r")) == NULL)
  19.               {
  20.                       fprintf(stderr, "Can not open the file: %s\n", filename);
  21.                       return 0;
  22.                   }
  23.                   
  24.                   while ((temp = fgetc(fp)) != EOF)
  25.                   {
  26.                                 if (temp == '\n')
  27.                                 {
  28.                                              count++;
  29.                                    }
  30.                   }
  31.                   
  32.                   fclose(fp);
  33.                   
  34.                   return count;
  35. }

  36. void findAllCodes(const char *path)
  37. {
  38.               struct _finddata_t fa;
  39.               long handle;
  40.               char thePath[MAX], target[MAX];
  41.               
  42.               strcpy(thePath, path);
  43.               if((handle = _findfirst(strcat(thePath, "/*.c"),&fa)) != -1L)
  44.               {
  45.                         do
  46.                         {
  47.                                 sprintf(target, "%s/%s", path, fa.name);
  48.                                 total += countLines(target);
  49.                            }while (_findnext(handle, &fa) == 0);
  50.                   }
  51.                   
  52.                   _findclose(handle);
  53. }

  54. void findALLdirs(const char *path)
  55. {
  56.               struct _finddata_t fa;
  57.               long handle;
  58.               char thePath[MAX];
  59.               
  60.               strcpy(thePath, path);
  61.               if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
  62.               {
  63.                           fprintf(stderr, "The path %s is wrong!\n", path);
  64.                           return;
  65.                   }
  66.                   
  67.                   do
  68.                   {
  69.                               if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  70.                               continue;
  71.                               
  72.                               if( fa.attrib == _A_SUBDIR)
  73.                               {
  74.                                           sprintf(thePath, "%s/%s, path, fa.name");
  75.                                           findAllCodes(thePath);
  76.                                           findALLdirs(thePath);
  77.                                   }
  78.                   }while (_findnext(handle, &fa) == 0);
  79.                   
  80.                   _findclose(handle);
  81. }

  82. int main()
  83. {
  84.              char path[MAX] = ".";
  85.             
  86.              printf("计算中...\n");
  87.             
  88.              findAllCodes(path);
  89.              findALLdirs(path);
  90.             
  91.              printf("目前你一共写了%ld 行代码!\n\n",total);
  92.              system("pause");
  93.             
  94.              return 0;
  95. }
复制代码

打字问题太多了,findALLDirs, findAllDirs, findALLdirs
一个函数三个名字
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-5 11:29:59 | 显示全部楼层
代码呢???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-5 12:06:53 | 显示全部楼层

#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[MAX], target[MAX];
             
             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[MAX];
             
             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[MAX] = ".";
            
            printf("计算中...\n");
            
            findAllCodes(path);
            findALLDirs(path);
            
            printf("目前你一共写了%ld 行代码!\n\n",total);
            system("pause");
            
            return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-5 12:28:25 | 显示全部楼层
1522843196 发表于 2020-3-5 12:06
#include
#include
#include

这是报错吧。。。。
等下,我帮你改改
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-5 12:31:07 | 显示全部楼层    本楼为最佳答案   
1522843196 发表于 2020-3-5 12:06
#include
#include
#include
  1. #include <io.h>
  2. #include <direct.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>

  6. #define MAX       256

  7. long total;

  8. int countLines(const char *filename);
  9. void findAllCodes(const char *path);
  10. void findALLfiles(const char *path);
  11. void findALLdirs(const char *path);

  12. int countLines(const char*filename)
  13. {
  14.               FILE *fp;
  15.               int count = 0;
  16.               int temp;
  17.               
  18.               if((fp = fopen(filename, "r")) == NULL)
  19.               {
  20.                       fprintf(stderr, "Can not open the file: %s\n", filename);
  21.                       return 0;
  22.                   }
  23.                   
  24.                   while ((temp = fgetc(fp)) != EOF)
  25.                   {
  26.                                 if (temp == '\n')
  27.                                 {
  28.                                              count++;
  29.                                    }
  30.                   }
  31.                   
  32.                   fclose(fp);
  33.                   
  34.                   return count;
  35. }

  36. void findAllCodes(const char *path)
  37. {
  38.               struct _finddata_t fa;
  39.               long handle;
  40.               char thePath[MAX], target[MAX];
  41.               
  42.               strcpy(thePath, path);
  43.               if((handle = _findfirst(strcat(thePath, "/*.c"),&fa)) != -1L)
  44.               {
  45.                         do
  46.                         {
  47.                                 sprintf(target, "%s/%s", path, fa.name);
  48.                                 total += countLines(target);
  49.                            }while (_findnext(handle, &fa) == 0);
  50.                   }
  51.                   
  52.                   _findclose(handle);
  53. }

  54. void findALLdirs(const char *path)
  55. {
  56.               struct _finddata_t fa;
  57.               long handle;
  58.               char thePath[MAX];
  59.               
  60.               strcpy(thePath, path);
  61.               if((handle = _findfirst(strcat(thePath, "/*"), &fa)) == -1L)
  62.               {
  63.                           fprintf(stderr, "The path %s is wrong!\n", path);
  64.                           return;
  65.                   }
  66.                   
  67.                   do
  68.                   {
  69.                               if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  70.                               continue;
  71.                               
  72.                               if( fa.attrib == _A_SUBDIR)
  73.                               {
  74.                                           sprintf(thePath, "%s/%s, path, fa.name");
  75.                                           findAllCodes(thePath);
  76.                                           findALLdirs(thePath);
  77.                                   }
  78.                   }while (_findnext(handle, &fa) == 0);
  79.                   
  80.                   _findclose(handle);
  81. }

  82. int main()
  83. {
  84.              char path[MAX] = ".";
  85.             
  86.              printf("计算中...\n");
  87.             
  88.              findAllCodes(path);
  89.              findALLdirs(path);
  90.             
  91.              printf("目前你一共写了%ld 行代码!\n\n",total);
  92.              system("pause");
  93.             
  94.              return 0;
  95. }
复制代码

打字问题太多了,findALLDirs, findAllDirs, findALLdirs
一个函数三个名字
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-5 13:56:00 | 显示全部楼层
好的,谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-6 04:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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