鱼C论坛

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

[已解决]C语言第一节课后习题运行不了

[复制链接]
发表于 2023-9-10 16:34:05 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<io.h>
  2. #include<direct.h>
  3. #include<stdlib.h>
  4. #include<stdio.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(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("目前你总共写了%ld行代码!\n\n", total);
  78.     system("pause");
  79.     return 0;
  80. }
复制代码

报错信息:
严重性        代码        说明        项目        文件        行        禁止显示状态
错误        C4996        'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.        S1        D:\自动化学习资料库\C语言\小甲鱼课后作业\S1\S1\源.c        17       
错误        C4996        'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.        S1        D:\自动化学习资料库\C语言\小甲鱼课后作业\S1\S1\源.c        37       
错误        C4996        'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.        S1        D:\自动化学习资料库\C语言\小甲鱼课后作业\S1\S1\源.c        38       
错误        C4996        'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.        S1        D:\自动化学习资料库\C语言\小甲鱼课后作业\S1\S1\源.c        42       
错误        C4996        'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.        S1        D:\自动化学习资料库\C语言\小甲鱼课后作业\S1\S1\源.c        54       
错误        C4996        'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.        S1        D:\自动化学习资料库\C语言\小甲鱼课后作业\S1\S1\源.c        55       
错误        C4996        'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.        S1        D:\自动化学习资料库\C语言\小甲鱼课后作业\S1\S1\源.c        66       

我用的软件是Visual Studio2019,对了好几遍找不到是哪出了问题 求各位大佬帮忙解答一下,感激不尽
最佳答案
2023-9-10 16:47:29
这些错误是由于使用了不安全的函数而导致的。Visual Studio 2019建议使用更安全的替代函数。

你可以按照编译器的建议进行修改,使用更安全的函数代替。
对于`fopen`,你可以使用`fopen_s`来替代:

  1. if (fopen_s(&fp, filename, "r") != 0)
  2. {
  3.     fprintf(stderr, "Can not open the file:%s\n", filename);
  4.     return 0;
  5. }
复制代码



对于`strcpy`和`strcat`,你可以使用`strcpy_s`和`strcat_s`来替代:

  1. strcpy_s(thePath, MAX, path);
  2. strcat_s(thePath, MAX, "/*.c");

复制代码

对于`sprintf`,你可以使用`sprintf_s`来替代:

  1. sprintf_s(target, MAX, "%s/%s", path, fa.name);
复制代码


这样修改后,你的代码应该可以在Visual Studio 2019中编译通过了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-9-10 19:53:46 | 显示全部楼层
改完之后还是显示找不到指定文件是什么意思??无法启动程序
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-10 16:34:20 | 显示全部楼层
报错信息显示,你使用的是不安全的函数。在最新的编译器中,某些函数被认为是不安全的,并建议使用替代函数以提高代码的安全性。

以下是替代函数的建议:

1. 将fopen替换为fopen_s函数。
   例如:if ((fopen_s(&fp, filename, "r")) != 0)

2. 将strcpy替换为strcpy_s函数。
   例如:strcpy_s(thePath, sizeof(thePath), path);

3. 将strcat替换为strcat_s函数。
   例如:strcat_s(thePath, sizeof(thePath), "/*.c");

4. 将sprintf替换为sprintf_s函数。
   例如:sprintf_s(target, sizeof(target), "%s/%s", path, fa.name);

你需要将这些函数替换为建议的安全函数,以解决报错问题。同时,确保你的编译器已经禁用了相关的警告。

另外,还请注意检查你的代码逻辑是否正确,确保文件路径和文件名没有问题,避免程序运行错误。
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-10 16:34:24 | 显示全部楼层
这些错误信息都是因为使用了不安全的函数,可以按照错误信息中给出的建议,使用对应的安全函数,或者在代码开头加上#pragma warning(disable:4996)来禁用这些警告。具体修改如下:

1. 将fopen改为fopen_s,修改后的代码如下:

  1. FILE* fp;
  2. int count = 0;
  3. int temp;
  4. if (fopen_s(&fp, filename, "r") != 0)
  5. {
  6.     fprintf(stderr, "Can not open the file:%s\n", filename);
  7.     return 0;
  8. }
复制代码


2. 将strcpy改为strcpy_s,修改后的代码如下:

  1. char thePath[MAX], target[MAX];
  2. strcpy_s(thePath, MAX, path);
复制代码


3. 将strcat改为strcat_s,修改后的代码如下:

  1. if ((handle = _findfirst(strcat_s(thePath, MAX, "/*.c"), &fa)) != -1L)
复制代码


4. 将sprintf改为sprintf_s,修改后的代码如下:

  1. sprintf_s(target, MAX, "%s/%s", path, fa.name);
复制代码


另外,建议在使用_findfirst函数前先判断路径是否存在,可以使用_access函数来判断,修改后的代码如下:

  1. if (_access(path, 0) == -1)
  2. {
  3.     fprintf(stderr, "The path %s does not exist!\n", path);
  4.     return 0;
  5. }
复制代码


修改后的完整代码如下:

  1. #include<io.h>
  2. #include<direct.h>
  3. #include<stdlib.h>
  4. #include<stdio.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 findALLDirs(const char* path);

  11. int countLines(const char* filename)
  12. {
  13.     FILE* fp;
  14.     int count = 0;
  15.     int temp;
  16.     if (fopen_s(&fp, filename, "r") != 0)
  17.     {
  18.         fprintf(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.     if (_access(path, 0) == -1)
  37.     {
  38.         fprintf(stderr, "The path %s does not exist!\n", path);
  39.         return;
  40.     }
  41.     strcpy_s(thePath, MAX, path);
  42.     if ((handle = _findfirst(strcat_s(thePath, MAX, "/*.c"), &fa)) != -1L)
  43.     {
  44.         do
  45.         {
  46.             sprintf_s(target, MAX, "%s/%s", path, fa.name);
  47.             total += countLines(target);
  48.         } while (_findnext(handle, &fa) == 0);

  49.     }
  50.     _findclose(handle);
  51. }

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

  81. int main()
  82. {
  83.     char path[MAX] = ".";
  84.     printf("计算中...\n");
  85.     findAllCodes(path);
  86.     findALLDirs(path);
  87.     printf("目前你总共写了%ld行代码!\n\n", total);
  88.     system("pause");
  89.     return 0;
  90. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-10 16:47:29 | 显示全部楼层    本楼为最佳答案   
这些错误是由于使用了不安全的函数而导致的。Visual Studio 2019建议使用更安全的替代函数。

你可以按照编译器的建议进行修改,使用更安全的函数代替。
对于`fopen`,你可以使用`fopen_s`来替代:

  1. if (fopen_s(&fp, filename, "r") != 0)
  2. {
  3.     fprintf(stderr, "Can not open the file:%s\n", filename);
  4.     return 0;
  5. }
复制代码



对于`strcpy`和`strcat`,你可以使用`strcpy_s`和`strcat_s`来替代:

  1. strcpy_s(thePath, MAX, path);
  2. strcat_s(thePath, MAX, "/*.c");

复制代码

对于`sprintf`,你可以使用`sprintf_s`来替代:

  1. sprintf_s(target, MAX, "%s/%s", path, fa.name);
复制代码


这样修改后,你的代码应该可以在Visual Studio 2019中编译通过了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2023-9-10 18:52:18 | 显示全部楼层
Mike_python小 发表于 2023-9-10 16:47
这些错误是由于使用了不安全的函数而导致的。Visual Studio 2019建议使用更安全的替代函数。

你可以按照 ...

好的,谢谢你!!我去试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 03:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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