【发布时间】:2014-11-14 10:57:30
【问题描述】:
我正在尝试从一个文件夹中读取多个文本文件,并存储每个单词的开始位置。我正在使用 Boost 从标点符号中清除文本。
当单词包含特殊字符(如(Õ、Ø、æ 等)时,我会遇到问题。 在这种情况下,我收到一条错误消息:"Expression: (unsigned)(c+1).
这是我提到的应用程序的代码:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include<iterator>
#include<string>
#include "/../dirent.h/dirent.h"
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
int main() {
DIR* dir;
dirent* pdir;
dir = opendir("D:/../dataset/");
int number_of_words=0;
int text_length = 30;
char filename[300];
int i=0;
while (pdir = readdir(dir))
{
string fileString;
cout<<"-------------------------------------------"<<endl;
cout<<"Name of text file: "<<pdir->d_name << endl;
strcpy(filename, "D:/.../dataset/");
strcat(filename, pdir->d_name);
ifstream file(filename);
std::istream_iterator<std::string> beg(file), end;
number_of_words = distance(beg,end);
//cout<<"Number of words in file: "<<number_of_words<<endl;
ifstream files(filename);
//char output[200];
if (file.is_open())
{
string output;
while (!files.eof())
{
files >> output;
fileString += " ";
fileString += output;
//cout<<output<<endl;
}
//cout<<fileString<<endl;
cout<<"Number of characters: "<<fileString.size()<<endl;
cout<<"-------------------------------------------"<<endl;
string fileStringTokenized;
tokenizer<>tok (fileString);
int indice_cuvant_curent = 0;
int index = 0;
vector<int> myvector;
for(tokenizer<>::iterator beg=tok.begin(); beg!=tok.end(); ++beg)
{
string currentWord;
currentWord = *beg;
myvector.push_back(index);
index+=currentWord.size();
//cout<<index<<"\t";
//cout<<*beg<<endl;
fileStringTokenized += *beg;
}
}
file.close();
}
closedir(dir);
return 0;
}
为什么会出现这个问题,我该如何解决?
【问题讨论】:
-
使用 unicode?创建一个最小的例子?我真的会把它复制粘贴到我的机器上并做一个例子,但我没有时间剥离你自己的代码并做你的工作。
-
也许可以试试
std::wstring(宽字符串)。另外:不要像那样使用while (!files.eof()),使用while (files >> output)- 请参阅here。