鱼C论坛

 找回密码
 立即注册
查看: 1571|回复: 11

[已解决]C问题

[复制链接]
发表于 2023-7-3 11:02:43 | 显示全部楼层 |阅读模式

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

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

x
vs2022运行(https://fishc.com.cn/thread-66283-1-1.html)win代码报错,手打和复制的都不行
最佳答案
2023-7-3 11:04:51
1. 报错信息?
2. c/c++不建议用vs2022
3. https://fishc.com.cn/thread-224194-1-1.html
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-3 11:04:51 | 显示全部楼层    本楼为最佳答案   
1. 报错信息?
2. c/c++不建议用vs2022
3. https://fishc.com.cn/thread-224194-1-1.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-3 11:06:11 | 显示全部楼层
报错信息没有,截图也没有
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-3 11:18:25 | 显示全部楼层
这段代码是一个用于统计指定路径下所有C语言源文件中的代码行数的程序。你提到该代码在VS 2022中报错,但没有具体说明错误的信息。在代码中,我注意到你使用了非标准的头文件`<io.h>`和`<direct.h>`,这些头文件在一些编译环境中可能不可用。

为了在VS 2022中运行这段代码,你可以进行以下步骤:

1. 删除`<io.h>`和`<direct.h>`头文件的包含语句。
2. 添加`<windows.h>`头文件,用于访问Windows系统函数和宏定义。

修改后的代码如下所示:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <windows.h>

  5. #define MAX        256

  6. long total;

  7. int countLines(const char *filename);
  8. void findAllCodes(const char *path);
  9. void findALLDirs(const char *path);

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

  34. void findAllCodes(const char *path)
  35. {
  36.         WIN32_FIND_DATA fa;
  37.         HANDLE handle;
  38.         char thePath[MAX], target[MAX];
  39.         
  40.         strcpy(thePath, path);
  41.         if((handle = FindFirstFile(strcat(thePath, "/*.c"), &fa)) != INVALID_HANDLE_VALUE)
  42.         {
  43.                 do
  44.                 {
  45.                         sprintf(target, "%s/%s", path, fa.cFileName);
  46.                         total += countLines(target);
  47.                 }while (FindNextFile(handle, &fa) != 0);
  48.         }
  49.    
  50.         FindClose(handle);
  51. }

  52. void findALLDirs(const char *path)
  53. {
  54.         WIN32_FIND_DATA fa;
  55.         HANDLE handle;
  56.         char thePath[MAX];
  57.         
  58.         strcpy(thePath, path);
  59.         if((handle = FindFirstFile(strcat(thePath, "/*"), &fa)) == INVALID_HANDLE_VALUE)
  60.         {
  61.                 fprintf(stderr, "The path %s is wrong!\n",path);
  62.                 return;
  63.         }
  64.    
  65.         do
  66.         {        
  67.                 if (!strcmp(fa.cFileName, ".") || !strcmp(fa.cFileName, ".."))
  68.                         continue;
  69.                     
  70.                 if( fa.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  71.                 {        
  72.                         sprintf(thePath, "%s/%s", path, fa.cFileName);
  73.                         findAllCodes(thePath);
  74.                         findALLDirs(thePath);
  75.                 }
  76.         }while (FindNextFile(handle, &fa) != 0);
  77.    
  78.         FindClose(handle);   
  79. }

  80. int main()
  81. {
  82.         char path[MAX] = ".";
  83.         
  84.         printf("Calculating...\n");
  85.         
  86.         findAllCodes(path);
  87.         findALLDirs(path);
  88.         
  89.         printf("Total lines of code: %ld\n\n", total);
  90.         system("pause");
  91.         
  92.         return 0;
  93. }
复制代码


请注意,我已经替换了Windows特定的文件和目录操作函数,以适应VS 2022编译环境。在代码中,我使用了`FindFirstFile`、`FindNextFile`和`FindClose`等函数来

如果回答有帮助,请给我设置最佳答案,求求了

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

使用道具 举报

发表于 2023-7-3 12:16:31 | 显示全部楼层
编程追风梦 发表于 2023-7-3 11:18
这段代码是一个用于统计指定路径下所有C语言源文件中的代码行数的程序。你提到该代码在VS 2022中报错,但没 ...

??????哪来的代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-3 12:20:51 | 显示全部楼层
sfqxx 发表于 2023-7-3 12:16
??????哪来的代码

肯定是链接里面小甲鱼的Win代码啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-4 17:42:45 | 显示全部楼层
编程追风梦 发表于 2023-7-3 11:18
这段代码是一个用于统计指定路径下所有C语言源文件中的代码行数的程序。你提到该代码在VS 2022中报错,但没 ...


                               
登录/注册后可看大图

还是报错,我换VsCode试下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-4 17:43:15 | 显示全部楼层
六翻了 发表于 2023-7-4 17:42
还是报错,我换VsCode试下

可以,给我个最佳答案把
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-4 17:45:10 | 显示全部楼层
编程追风梦 发表于 2023-7-3 11:18
这段代码是一个用于统计指定路径下所有C语言源文件中的代码行数的程序。你提到该代码在VS 2022中报错,但没 ...

>D:\code\Project2\test.c(122,15): error 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.
1>D:\code\Project2\test.c(148,33): warning C4133: “函数”: 从“char *”到“LPCWSTR”的类型不兼容
1>D:\code\Project2\test.c(152,29): warning C4477: “sprintf”: 格式字符串“%s”需要类型“char *”的参数,但可变参数 2 拥有了类型“WCHAR *”
1>D:\code\Project2\test.c(152,29): message : 请考虑在格式字符串中使用“%ls”
1>D:\code\Project2\test.c(152,29): message : 请考虑在格式字符串中使用“%lls”
1>D:\code\Project2\test.c(152,29): message : 请考虑在格式字符串中使用“%Ls”
1>D:\code\Project2\test.c(152,29): message : 请考虑在格式字符串中使用“%ws”
1>D:\code\Project2\test.c(147,5): error 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.
1>D:\code\Project2\test.c(148,33): error 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.
1>D:\code\Project2\test.c(152,13): error 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.
1>D:\code\Project2\test.c(167,33): warning C4133: “函数”: 从“char *”到“LPCWSTR”的类型不兼容
1>D:\code\Project2\test.c(175,23): warning C4133: “函数”: 从“WCHAR [260]”到“const char *”的类型不兼容
1>D:\code\Project2\test.c(175,53): warning C4133: “函数”: 从“WCHAR [260]”到“const char *”的类型不兼容
1>D:\code\Project2\test.c(180,30): warning C4477: “sprintf”: 格式字符串“%s”需要类型“char *”的参数,但可变参数 2 拥有了类型“WCHAR *”
1>D:\code\Project2\test.c(180,30): message : 请考虑在格式字符串中使用“%ls”
1>D:\code\Project2\test.c(180,30): message : 请考虑在格式字符串中使用“%lls”
1>D:\code\Project2\test.c(180,30): message : 请考虑在格式字符串中使用“%Ls”
1>D:\code\Project2\test.c(180,30): message : 请考虑在格式字符串中使用“%ws”
1>D:\code\Project2\test.c(166,5): error 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.
1>D:\code\Project2\test.c(167,33): error 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.
1>D:\code\Project2\test.c(180,13): error 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.
1>已完成生成项目“Project2.vcxproj”的操作 - 失败。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-7-4 17:47:23 | 显示全部楼层
六翻了 发表于 2023-7-4 17:45
>D:\code\Project2\test.c(122,15): error C4996: 'fopen': This function or variable may be unsafe. C ...

这些错误和警告信息是来自于编译器(在这种情况下是C编译器)生成的。它们指出了一些代码中可能存在的问题,并提供了一些建议来解决这些问题。

下面是你提供的错误和警告信息的一些解释以及对应的解决方法:

1. `error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead.`:这个错误提醒你使用了不安全的函数 `fopen`,建议使用更安全的 `fopen_s` 替代它。你可以将 `fopen` 改为 `fopen_s`。

2. `warning C4133: “函数”: 从“char *”到“LPCWSTR”的类型不兼容`:这个警告提示你的代码中存在字符类型的转换问题,从 `char*` 类型到 `LPCWSTR` 类型的转换不兼容。你需要确保转换的正确性,或者修改相关的代码以避免这种转换。

3. `warning C4477: “sprintf”: 格式字符串“%s”需要类型“char *”的参数,但可变参数 2 拥有了类型“WCHAR *”`:这个警告指出了 `sprintf` 函数中格式字符串 `%s` 需要一个 `char*` 类型的参数,但提供的参数是 `WCHAR*` 类型。你可以考虑使用 `%ls` 或者其他相应的格式字符串来匹配参数类型。

4. `error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.`:这个错误提示你使用了不安全的函数 `strcpy`,建议使用更安全的 `strcpy_s` 替代它。你可以将 `strcpy` 改为 `strcpy_s`。

5. `error C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead.`:这个错误提示你使用了不安全的函数 `strcat`,建议使用更安全的 `strcat_s` 替代它。你可以将 `strcat` 改为 `strcat_s`。

6. `error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead.`:这个错误提示你使用了不安全的函数 `sprintf`,建议使用更安全的 `sprintf_s` 替代它。你可以将 `sprintf` 改为 `sprintf_s`。

解决这些问题的方法是根据编译器的建议进行修改,使用建议中提到的更安全的函数或者采取其他适当的措施来解决类型不匹配的问题。

请注意,如果你的代码确实需要使用不安全的函数,你可以使用预处理指令 `_CRT_SECURE_NO_WARNINGS` 来禁用相关的警告。例如,在源文件的开头添加以下行可以禁用这些特定警告:

  1. #define _CRT_SECURE_NO_WARNINGS
复制代码


这样可以避免编译器生成与不安全函数相关

求求给个最佳答案

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

使用道具 举报

 楼主| 发表于 2023-7-4 18:43:30 | 显示全部楼层
按教程安装VSCode后(https://fishc.com.cn/thread-224194-1-1.html
直接复制win代码,(https://fishc.com.cn/thread-66283-1-1.html
不报错,但显示:璁$畻涓?..
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-7-4 20:18:09 | 显示全部楼层
歌者文明清理员 发表于 2023-7-3 11:04
1. 报错信息?
2. c/c++不建议用vs2022
3. https://fishc.com.cn/thread-224194-1-1.html

按教程安装好VSCode,调好了设置
直接复制代码,只输出第一个printf
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-12 07:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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