// FileEncoding.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <wchar.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
wchar_t linex[100];
FILE * f1;
//f1=_wfopen(L"C:\\uni.txt",L"rt+,ccs=UNICODE"); //or UTF-8
//f1= _wfopen(L"C:\\uni.txt",L"rt+,ccs=UTF-8");
int fileOpen=_wfopen_s(&f1,L"C:\\uni.txt",L"rt+,ccs=UTF-8");
//locale loc("");
//wcout.imbue(loc);
while (!feof(f1))
{
fgetws(linex,100,f1);
wcout<<linex;
}
fclose(f1);
return 0;
}
也可以使用int fileOpen=fopen_s(&f1, "C:\\uni.txt","rt+,ccs=UTF-8");
uni.txt中的内容是:this is a test
爱死msdn了,在VS2012中使用_wfopen会报错,而_wfopen_s通过,可参考:
http://msdn.microsoft.com/zh-cn/library/z5hh6ee9(v=vs.90).aspx