| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
- #include<stdio.h>
 
 - FILE* stream;
 
 - void main(void)
 
 - {
 
 -     long l;
 
 -     float fp;
 
 -     char s[81];
 
 -     char c;
 
 -     stream = fopen("fscanf.txt","r");
 
 -     if(stream == NULL)
 
 -     {
 
 -         printf("the file is opened error!\n");
 
 -     }
 
 -     else
 
 -     {
 
 -         fscanf(stream,"%s",s);
 
 -         fscanf(stream,"%ld",&l);
 
 -         fscanf(stream,"%f",&fp);
 
 -         fscanf(stream," %c",&c);
 
 -         printf("%s\n",s);
 
 -         printf("%ld\n",l);
 
 -         printf("%f\n",fp);
 
 -         printf("%c\n",c);
 
 -         fclose(stream);  /*关闭*/
 
 -     }
 
 - }
 
  复制代码 
 
如题 文件中内容为: 
 
字符串 92222 3.14 s 
 
在读取中文的时候就会乱码 如何解决呢
 本帖最后由 jackz007 于 2021-6-22 12:40 编辑 
- #include<stdio.h>
 
  
- int main(void)
 
 - {
 
 -         FILE * stream                                           ;
 
 -         char s[81] , c                                          ;
 
 -         long long l                                             ;
 
 -         if ((stream = fopen("fscanf.txt","r")) != NULL) {
 
 -                 fscanf(stream , "%s %lld %c" , s , & l , & c)   ;
 
 -                 printf("%s %lld %c\n" , s , l , c)              ;
 
 -                 fclose(stream)                                  ;
 
 -         } else {
 
 -                 fprintf(stderr , "the file is opened error!\n") ;
 
 -         }
 
 - }
 
  复制代码
        如果用上面的代码还是看不到中文字符,八成是汉字编码问题,可以通过以下方法进行确定: 
        如果是在 Windows 下,那就 CMD 到 "fscanf.txt" 所在路径,键入这个命令:
 
        如果是在 Linux 下,那就 CD 到 "fscanf.txt" 所在路径,键入这个命令:
 
        然后看看系统响应的内容是否是:
 
        如果不是,那就足以肯定,是 "fscanf.txt" 的汉字编码存在问题。  
 
 
 |   
 
 
 
 |