您的位置:首页 > 编程语言 > C语言/C++

C++ 读取中文文本 ifstream

2015-03-05 16:57 3047 查看
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
using namespace std;

int main()
{
string str = "";
wchar_t wch = '中';
char ch = 'b';
cout<<"char : "<<ch<<wch<<endl;

char filename[MAX_PATH];
GetModuleFileName(NULL,filename, MAX_PATH);
str = (string)filename;
int pos = str.find_last_of('\\',str.length());
str = str.substr(0,pos);
char * ch2 = const_cast<char *>(str.c_str());
cout<<"file path: "<< ch2<<endl;

str += "\\read.txt";

ifstream ifs;
ifs.open(str);
if (!ifs.is_open())
{
cout<<"open file "<<str<<"	failed"<<endl;
}
else
{
string con = "";
int count = 0;
getline(ifs,con,':');
while (ifs)
{
++count;
cout<<count<<":	"<<con<<endl;
getline(ifs,con,':');
}
cout<<"Done!"<<endl;
}
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: