鱼C论坛

 找回密码
 立即注册
查看: 1900|回复: 7

[已解决]c语言运行出现乱码

[复制链接]
发表于 2019-5-9 00:53:12 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 大仙爱吃糖 于 2019-5-9 01:33 编辑

c程序在windows环境下编译,运行的时候出现乱码
百度方法未解决,希望各位能帮助一下

代码:带你学c带你飞  第一季 第一课的练习

统计当前目录及所有子目录下,C语言源文件的代码总行数

  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 findALLDirs(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.         {
  62.             continue;
  63.         }

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

  71.     _findclose(handle);
  72. }

  73. int main()
  74. {
  75.     char path[MAX] = ".";

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

  77.     findAllCodes(path);
  78.     findALLDirs(path);

  79.     printf("目前你总共写了 %1d 行代码!\n",total);
  80.     system("pause");

  81.     return 0;
  82. }
复制代码

0.png
最佳答案
2019-5-9 11:59:47
看一下 C 文件的字符编码, win命令行默认只能输出 GB 系列编码的中文, UTF 等其他编码就会乱码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-9 00:58:06 | 显示全部楼层

回帖奖励 +5 鱼币

本帖最后由 jackz007 于 2019-5-9 01:08 编辑

      楼主你不能这么懒,要把你的全部代码都贴出来
      根据你贴出来的信息来看,你应该是把 printf() 里面的格式描述符 "%s" 统统写成了 "&s","%ld" 写成了 "&ld" ,也就是说,你把格式描述符的关键符号 "%" 统统写成了 "&" 这是非常要命的问题,必须逐一更正。
       再更正你一个错误,你的情况是程序运行出现乱码,而不能描述成"C语言编译出现乱码",首先,编译过程不可能出现乱码,再说,你的乱码是出现在程序运行过程中,与编译程序和编译过程能有什么关系?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-9 01:22:28 | 显示全部楼层
jackz007 发表于 2019-5-9 00:58
楼主你不能这么懒,要把你的全部代码都贴出来
      根据你贴出来的信息来看,你应该是把 printf()  ...

好的,谢谢指正错误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-9 11:59:47 | 显示全部楼层    本楼为最佳答案   

回帖奖励 +5 鱼币

看一下 C 文件的字符编码, win命令行默认只能输出 GB 系列编码的中文, UTF 等其他编码就会乱码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-9 15:57:58 | 显示全部楼层
大C神 发表于 2019-5-9 11:59
看一下 C 文件的字符编码, win命令行默认只能输出 GB 系列编码的中文, UTF 等其他编码就会乱码

好的,改过来了,多谢,已经显示正常了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-9 18:44:31 | 显示全部楼层

回帖奖励 +5 鱼币

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

使用道具 举报

发表于 2020-6-14 14:43:27 | 显示全部楼层

回帖奖励 +5 鱼币

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

使用道具 举报

发表于 2020-11-3 11:41:33 | 显示全部楼层

回帖奖励 +5 鱼币

我当时也出了好多问题,不过都改好了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-3 12:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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