【发布时间】:2014-02-18 11:14:43
【问题描述】:
。 我是 C++ 新手。我必须找出用户传递的文件包含的编码类型。但我不知道如何检查文件的编码。所以我需要打印文件是unicode还是ansi或unicode big endian还是utf8。我搜索了很多但找不到解决方案。直到现在我已经打开了一个文件:
#include "stdafx.h"
#include <iostream.h>
#include <stdio.h>
#include<conio.h>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
fstream f;
f.open("c:\abc.txt", fstream::in | fstream::out); /* Read-write. */
getch();
return 0;
}
所以请谁能告诉我这个的代码解决方案。
如果我正在访问记事本文件怎么办?
提前谢谢..
【问题讨论】:
-
你不能只找出正在使用的编码,你只能预测它。您应该阅读以下内容:joelonsoftware.com/articles/Unicode.html
-
如果文件有 BOM,则使用 BOM。如果没有,请询问用户。这是唯一确定的方法。
-
@jonathan 我如何检查文件中的 BOM,你能解释一下吗? .
-
记得在你传递的路径中转义你的 \ 以打开或只是做我所做的并使用 / 而不是 \ 路径。
标签: c++