鱼C论坛

 找回密码
 立即注册
查看: 2958|回复: 10

求大佬帮看看,C语言S1E2第一次作业

[复制链接]
发表于 2022-10-9 15:29:04 | 显示全部楼层 |阅读模式

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

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

x
小甲鱼给的答案是

计算中...
目前你总共写了101行代码

但我运行的结果是

计算中...

就少了后面那一句。文件后缀也改成了.c了,眼睛都看花了也没找出来代码有什么不一样的。用的是vs2022,求大佬帮忙,十分感谢!
作业原帖:https://fishc.com.cn/forum.php?m ... peid%26typeid%3D570


  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. int countLines(const char *filename)
  12. {
  13.         FILE *fp;
  14.         int count = 0;
  15.         int temp;

  16.         if ((fp = fopen(filename, "r")) == NULL)
  17.         {
  18.                 fprintf_s(stderr, "Can not open the file: %s\n", filename);
  19.                 return 0;
  20.         }

  21.         while ((temp = fgetc(fp)) != EOF)
  22.         {
  23.                 if (temp == '\n')
  24.                 {
  25.                         count++;
  26.                 }
  27.         }

  28.         fclose(fp);

  29.         return count;
  30. }

  31. void findAllCodes(const char* path)
  32. {
  33.         struct _finddata_t fa;
  34.         long handle;
  35.         char thePath[MAX], target[MAX];

  36.         strcpy_s(thePath, path);
  37.         if ((handle = _findfirst(strcat(thePath, "/*.c"), &fa)) != -1L)
  38.         {
  39.                 do
  40.                 {
  41.                         sprintf(target, "%s/%s", path, fa.name);
  42.                         total += countLines(target);
  43.                 } while (_findnext(handle, &fa) == 0);
  44.         }

  45.         _findclose(handle);
  46. }

  47. void findALLDirs(const char *path)
  48. {
  49.         struct _finddata_t fa;
  50.         long handle;
  51.         char thePath[MAX];

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

  58.         do
  59.         {
  60.                 if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  61.                         continue;

  62.                 if( fa.attrib == _A_SUBDIR)
  63.                 {
  64.                         sprintf(thePath, "%s/%s", path, fa.name);
  65.                         findAllCodes(thePath);
  66.                         findALLDirs(thePath);
  67.                 }
  68.         } while (_findnext(handle, &fa) == 0);

  69.         _findclose(handle);
  70. }

  71. int main()
  72. {
  73.         char path[MAX] = ".";

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

  75.         findAllCodes(path);
  76.         findALLDirs(path);

  77.         printf_s("目前你总共写了 %l        d 行代码!\n\n", total);
  78.         system("pause");

  79.         return 0;
  80. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-9 15:38:31 | 显示全部楼层
本帖最后由 jackz007 于 2022-10-9 15:39 编辑

       这一行能不能看出问题?
  1.        printf_s("目前你总共写了 %l        d 行代码!\n\n", total);
复制代码

       显然应该是这样
  1.        printf_s("目前你总共写了 %ld 行代码!\n\n", total);
复制代码

       代码没有问题,只要改了就好了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-9 15:45:01 | 显示全部楼层
本帖最后由 jhq999 于 2022-10-9 15:47 编辑

  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. int countLines(const char *filename)
  12. {
  13.         FILE *fp;
  14.         int count = 0;
  15.         int temp;

  16.         if ((fp = fopen(filename, "r")) == NULL)
  17.         {
  18.                 fprintf_s(stderr, "Can not open the file: %s\n", filename);
  19.                 return 0;
  20.         }

  21.         while ((temp = fgetc(fp)) != EOF)
  22.         {
  23.                 if (temp == '\n')
  24.                 {
  25.                         count++;
  26.                 }
  27.         }

  28.         fclose(fp);

  29.         return count;
  30. }

  31. void findAllCodes(const char* path)
  32. {
  33.         struct _finddata_t fa;
  34.         long handle;
  35.         char thePath[MAX], target[MAX];

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

  45.         _findclose(handle);
  46. }

  47. void findALLDirs(const char *path)
  48. {
  49.         struct _finddata_t fa;
  50.         long handle;
  51.         char thePath[MAX];

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

  58.         do
  59.         {
  60.                 if (!strcmp(fa.name, ".") || !strcmp(fa.name, ".."))
  61.                         continue;

  62.                 if( fa.attrib == _A_SUBDIR)
  63.                 {
  64.                         sprintf(thePath, "%s/%s", path, fa.name);
  65.                         findAllCodes(thePath);
  66.                         findALLDirs(thePath);
  67.                 }
  68.         } while (_findnext(handle, &fa) == 0);

  69.         _findclose(handle);
  70. }

  71. int main()
  72. {
  73.         char path[MAX] = ".";

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

  75.         findAllCodes(path);
  76.         findALLDirs(path);

  77.         printf_s("目前你总共写了 %d行代码!\n\n", total);////////////////
  78.         system("pause");

  79.         return 0;
  80. }
复制代码
  1. 计算中...
  2. 目前你总共写了 102行代码!

  3. 请按任意键继续. . .
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-9 23:35:08 | 显示全部楼层
我不知道你用的是哪个包哈,我用的C++系列的包,源文件后缀就是.cpp而不是.c,C语言我不是很清楚。
另外有一点值得注意,VS里边生成的可执行文件(.exe)和代码源文件(.c或者.cpp)好像不在一个目录下,所以文件名必须带路径,也就是你main函数下的path字符串是要带路径的。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-11 14:42:24 | 显示全部楼层
jackz007 发表于 2022-10-9 15:38
这一行能不能看出问题?

       显然应该是这样

vs上看是正常的,复制过来不知道为什么变这样了。刚刚把你的复制上去了,还是原来那样,但是感谢大佬帮助
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-11 14:48:18 | 显示全部楼层

感谢帮忙,但好像还是行不通
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-11 14:50:32 | 显示全部楼层
BPN-06 发表于 2022-10-11 14:42
vs上看是正常的,复制过来不知道为什么变这样了。刚刚把你的复制上去了,还是原来那样,但是感谢大佬帮助 ...


        如果没有改观,那就至少可以说明,你编译好的 exe 文件没有和你的 .c 的源代码文件处在同一个目录。你可以把源代码文件复制到 exe 文件的目录下,也可以把随便一个文本文件复制到 exe 目录下,并把文件扩展名改为 .c,然后再试一下。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-11 15:38:42 | 显示全部楼层
摸鱼的兔崽子 发表于 2022-10-9 23:35
我不知道你用的是哪个包哈,我用的C++系列的包,源文件后缀就是.cpp而不是.c,C语言我不是很清楚。
另外有 ...

我的源文件后缀原本是.cpp,之前看了别人的帖子然后改成了.c。大佬救一救,path字符串怎么带路径。我有看到一个帖子写的代码是 with open(r'路径’),大标题是《path路径的写法》,小标题是 绝对路径。我也不知道是不是,但我也找不到 看起来更有用的帖了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-11 16:32:09 | 显示全部楼层
jackz007 发表于 2022-10-11 14:50
如果没有改观,那就至少可以说明,你编译好的 exe 文件没有和你的 .c 的源代码文件处在同一个 ...

好像还是不可以,我是憨憨。我在原来的项目A里的解决方案资源管理器,右击源文件,新建了一个项目B.c,然后移除了A.cpp,再把B.c放到含有A.exe的debug文件夹里,这样会有问题吗?直接把A.cpp改成A.c会显示找不到文件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-11 16:34:35 | 显示全部楼层
BPN-06 发表于 2022-10-11 16:32
好像还是不可以,我是憨憨。我在原来的项目A里的解决方案资源管理器,右击源文件,新建了一个项目B.c,然 ...

        难道你真的不知道自己编译好的 exe 文件存在哪个目录???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-12 00:50:59 | 显示全部楼层
BPN-06 发表于 2022-10-11 15:38
我的源文件后缀原本是.cpp,之前看了别人的帖子然后改成了.c。大佬救一救,path字符串怎么带路径。我有看 ...

既然你的源文件后缀是cpp那就用cpp,不要用c,因为别人的环境不一样,软件不一样,所以源文件格式不一样,.c应该是linux下C语言的后缀。
路径问题,可以采用绝对路径,就是把要查代码的文件夹打开,然后复制文件地址(从盘开始的,比如C:\叽哩哇啦\莫西莫西)然后贴进去,你原来那个“.”我不是很明白,因为我对这套代码不是很熟。也可以采用相对路径,就是以“..”作为一个符号表明返回上一级文件夹,比如exe在/a/b/c这个目录下边,但是要查的源文件在/a下边,叫做“what.cpp”,那在fopen的时候给的完整路径就是“..\\..\\what.cpp”,这叫做相对路径。具体可以在csdn查一下C语言相对路径的使用,大部分讲的还是很清楚的。
你这个代码我不知道为啥路径寻找老是报错,不管是相对路径还是绝对路径都报错,应该是那个FindAllDirs函数和这个方法之间有不匹配,这我就不是很清楚了,因为我这个函数真没看懂。
但是我另写了一个简单的程序试了一下,单纯使用fopen去查某个特定的文件的行数,用绝对路径相对路径都是没有问题的。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 18:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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