首页 > 代码库 > 一个关于编码的实验(C#写的记事本文档,在Linux下用C++读取)
一个关于编码的实验(C#写的记事本文档,在Linux下用C++读取)
第一步:用C#用各种类型的编码生成txt文档
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace WordTest { class Program { static void Main(string[] args) { string text1 = "wa jue ji ji shu dao di na jia qiang?\r\n"; string text2 = "挖 掘 机 技 术 到 底 哪 家 强?\r\n"; string text = text1 + text2; using (StreamWriter sw = new StreamWriter( "testASCII.txt", false, Encoding.ASCII)) { sw.Write(text); Console.WriteLine("Encoding.ASCII 书写完毕"); } using (StreamWriter sw = new StreamWriter( "testBigEndianUnicode.txt", false, Encoding.BigEndianUnicode)) { sw.Write(text); Console.WriteLine("Encoding.BigEndianUnicode 书写完毕"); } using (StreamWriter sw = new StreamWriter( "testUnicode.txt", false, Encoding.Unicode)) { sw.Write(text); Console.WriteLine("Encoding.Unicode 书写完毕"); } using (StreamWriter sw = new StreamWriter( "testUTF32.txt", false, Encoding.UTF32)) { sw.Write(text); Console.WriteLine("Encoding.UTF32 书写完毕"); } using (StreamWriter sw = new StreamWriter( "testUTF7.txt", false, Encoding.UTF7)) { sw.Write(text); Console.WriteLine("Encoding.UTF7 书写完毕"); } using (StreamWriter sw = new StreamWriter( "testUTF8.txt", false, Encoding.UTF8)) { sw.Write(text); Console.WriteLine("Encoding.UTF8 书写完毕"); } Console.ReadLine(); } } }
程序运行结果:
第二步:复制到一个RedHat系统中,用cat命令读出这些txt文档的内容
第三步:用C++读出这些txt文档的内容
程序代码:
#include <iostream> #include <fstream> using namespace std; int main() { ifstream cin1("testASCII.txt"); char s1a[100]; char s1b[100]; cin1.getline(s1a, 100, ‘\n‘); cin1.getline(s1b, 100, ‘\n‘); cout << "ASCII:" << endl << s1a << endl << s1b << endl << endl; ifstream cin2("testBigEndianUnicode.txt"); char s2a[100]; char s2b[100]; cin2.getline(s2a, 100, ‘\n‘); cin2.getline(s2b, 100, ‘\n‘); cout << "BigEndianUnicode" << endl << s2a << endl << s2b << endl << endl; ifstream cin3("testUnicode.txt"); char s3a[100]; char s3b[100]; cin3.getline(s3a, 100, ‘\n‘); cin3.getline(s3b, 100, ‘\n‘); cout << "Unicode" << endl << s3a << endl << s3b << endl << endl; ifstream cin4("testUTF32.txt"); char s4a[100]; char s4b[100]; cin4.getline(s4a, 100, ‘\n‘); cin4.getline(s4b, 100, ‘\n‘); cout << "UTF32" << endl << s4a << endl << s4b << endl << endl; ifstream cin5("testUTF7.txt"); char s5a[100]; char s5b[100]; cin5.getline(s5a, 100, ‘\n‘); cin5.getline(s5b, 100, ‘\n‘); cout << "UTF7" << endl << s5a << endl << s5b << endl << endl; ifstream cin6("testUTF8.txt"); char s6a[100]; char s6b[100]; cin6.getline(s6a, 100, ‘\n‘); cin6.getline(s6b, 100, ‘\n‘); cout << "UTF8" << endl << s6a << endl << s6b << endl << endl; return 0; }
运行结果:
结论:用ASCII编码的文件,可以读取英文字符,用UTF8编码的文件,在Linux下的C++可以读取中文和英文
END
一个关于编码的实验(C#写的记事本文档,在Linux下用C++读取)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。