截取了今天的一篇中文新闻和英文新闻作为test文件,分别使用ASNI,UTF-8等不同编码模式。
使用如下代码读入一段文字然后输出到屏幕上。
#include <stdio.h> #include <string.h>
int main() { ?? ?char Border[100]={"---------------------------------------------------------------------------------------"}; ?? ?FILE *fp1 = NULL; ?? ?char buff[500] = {""}; ?? ?fp1 = fopen("E:\\C\\paypal-ANSI.txt","r"); ?? ?fgets(buff,50,fp1); ?? ?printf("paypal-ANSI.txt\n%s\n%s\n",buff,Border); ?? ?fclose (fp1); ?? ?fp1 = fopen("E:\\C\\paypal-BOMUTF8.txt","r"); ?? ?fgets(buff,50,fp1); ?? ?printf("paypal-BOMUTF8.txt\n%s\n%s\n",buff,Border); ?? ?fclose (fp1); ?? ?fp1 = fopen("E:\\C\\paypal-UTF-8.txt","r"); ?? ?fgets(buff,50,fp1); ?? ?printf("paypal-UTF-8.txt\n%s\n%s\n",buff,Border); ?? ?fclose (fp1); ?? ?fp1 = fopen("E:\\C\\paypal-UTF16BE.txt","r"); ?? ?fgets(buff,50,fp1); ?? ?printf("paypal-UTF16BE.txt\n%s\n%s\n",buff,Border); ?? ?fclose (fp1); ?? ?fp1 = fopen("E:\\C\\paypal-UTF16LE.txt","r"); ?? ?fgets(buff,50,fp1); ?? ?printf("paypal-UTF16LE.txt\n%s\n%s\n",buff,Border); ?? ?fclose (fp1); ?? ?// ?? ?fp1 = fopen("E:\\C\\ENpaypal-ANSI.txt","r"); ?? ?fgets(buff,50,fp1); ?? ?printf("ENpaypal-ANSI.txt\n%s\n%s\n",buff,Border); ?? ?fclose (fp1); ?? ?fp1 = fopen("E:\\C\\ENpaypal-BOMUTF8.txt","r"); ?? ?fgets(buff,50,fp1); ?? ?printf("ENpaypal-BOMUTF8.txt\n%s\n%s\n",buff,Border); ?? ?fclose (fp1); ?? ?fp1 = fopen("E:\\C\\ENpaypal-UTF-8.txt","r"); ?? ?fgets(buff,50,fp1); ?? ?printf("ENpaypal-UTF-8.txt\n%s\n%s\n",buff,Border); ?? ?fclose (fp1); ?? ?fp1 = fopen("E:\\C\\ENpaypal-UTF16BE.txt","r"); ?? ?fgets(buff,50,fp1); ?? ?printf("ENpaypal-UTF16BE.txt\n%s\n%s\n",buff,Border); ?? ?fclose (fp1); ?? ?fp1 = fopen("E:\\C\\ENpaypal-UTF16LE.txt","r"); ?? ?fgets(buff,50,fp1); ?? ?printf("ENpaypal-UTF16LE.txt\n%s\n%s\n",buff,Border); ?? ?fclose (fp1);
?? ?return 0; }
结果如下
可见红色箭头标记的输出没有产生乱码,中文(ASNI),英文(ASNI、UTF-8)
一般新建的txt文件默认编码格式为UTF-8,所以只要手动保存为ASNI编码格式就好了。
?修改位置在这里。
?
|