关于LINUX FILE命令是如何判断文件字符集的

今天在使用file -i 查看MYSQLDUMP文件的时候其输出为
charset=us-ascii with very long lines
我导出的文件应该是utf8的,为什么会显示ASCII呢,我们知道ASCII并没有中文编码,那么真的有问题吗?
然后用如下2个小程序测试了一下

点击(此处)折叠或打开

  1. #include<stdio.h>


  2. int main(void)
  3. {
  4.         FILE *p;
  5.         int i=0;
  6.         p=fopen("test11.txt","w+");
  7.         fputs("高鹏\n",p);
  8.         while(i<50000000)
  9.         {
  10.                 fputs("test",p);
  11.                 i++;
  12.         }

  13.         fputs("\n",p);
  14.         fclose(p);
  15.         return 0;
  16. }


点击(此处)折叠或打开

  1. #include<stdio.h>


  2. int main(void)
  3. {
  4.         FILE *p;
  5.         int i=0;
  6.         p=fopen("test10.txt","w+");
  7.         while(i<50000000)
  8.         {
  9.                 fputs("test",p);
  10.                 i++;
  11.         }

  12.         fputs("\n",p);
  13.         fputs("高鹏\n",p);
  14.         fclose(p);
  15.         return 0;
  16. }
实际上并没有什么不同这两个文件 test10 和test11 都有2行其中一个很长的行全部是test字符串,第二行是高鹏
明显他们应该返回UTF8编码,但是并不是
gaopeng@bogon:~$ file -i test10.txt 
test10.txt: text/plain; charset=us-ascii
gaopeng@bogon:~$ file -i test11.txt 
test11.txt: text/plain; charset=utf-8

可以看到如果"高鹏"字符串在第二行返回为ASCII而在第一行为UTF-8,我们可以推测出 file 命令是检测文件开头的某些字符而返回的,并没有全部查看,或者有什么其他算法,但是他不是全部查看。试想如果全部查看一遍 一个200G的备份文件瞬间就返回了结果也是不可能的。
在file的帮助中也明确的写着
   Once file has determined the character set used in a text-type file, it will attempt to determine in what language the file is written.  The language tests
     look for particular strings (cf.  ) that can appear anywhere in the first few blocks of a file.

当然要知道具体的方法估计只有源码。

所以file -i检测的并不一定是正确的字符集。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7728585/viewspace-2125168/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7728585/viewspace-2125168/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值