【发布时间】:2015-03-21 12:21:12
【问题描述】:
在我的程序中,我正在使用 ifstream 读取一个文本文件,以便使用 stringstream 读取每一行(使用 getline 进行标记化)打开它;当我得到一个欧洲字符时,比如“è”,它用“├¿”保存这个字符,这可以按预期工作,因为我使用的是字符串而不是 wstring。 但是当我从 cmd 得到一行时(我使用的是 Windows),单词“è”在字符串中被保存为“è”。我的目标是比较从文件和命令 shell 读取的字符串,但如果它们以不同的方式编码,我会被卡住,因为 "è".compare("├¿") 自然是 != 0。我想拥有两者都“错误”或都正确,因为我的目标不是显示它们,而只是计算发生次数。我正在使用最新版本的 Code::Blocks 进行编程,使用 MinGW 32 位和 gcc 4.7.1
更新(代码)
ifstream file;
stringstream stream;
file.open(path);
while( file ){
while( getline(file,line) ){
it = 1;
stream << line;
if( line.compare("")!=0 ){
while( getline(stream,token,'\t')) {
if( it == 1 ){
ID = atoi( token.c_str() );
}
if( it == 2 ){
word = token;
if( !case_sensitive ){
word = get_lower_case( word );
}
}
if( it == tags_index ){
pos = token;
}
it++;
}
data.push_back(make_row(ID,word,pos));
}
stream.clear();
}
}
这是我用来读取文件的函数的一部分(我有一个结构来存储列表文件的每个条目,我的问题是“单词”)。
getline(cin,sentence);
[...]
stringstream stream;
string token;
vector<string> tokens;
stream << sentence;
while( getline(stream,token,' ') ){
tokens.push_back(token);
}
stream.clear();
这就是我在命令外壳中读取输入流的方式。
【问题讨论】: