Reading in UTF-8 Files
I'm using VS 2008 and need to read text files that have UTF-8 Chinese characters. The file is organized like this: Each line contains a document, and the documents are tab delimited (index 'tab' doc title 'tab' doc body). So what I need to do is separate the rows on tabs then separate the third column (doc body) by spaces and store each word in a vector. All of this works fine when the file is ANSI encoded. But when it is UTF-8 it throws and Assertion Failure (unsigned)(c+1)<=256. I would like to keep the current functionality and flow, and use as little third party libraries as possible.
I have looked at different methods (ustream, wstream, etc.) but I'm a bit confused on how to actually use them.
Here is the method that reads in the files:
bool TabDelimitedSource::setup_next_buff_reader() {
this->current_source_file += 1;
bool no_more_files = false; // assume we have no more files by default
/** If there are still files int the directory load the next file*/
if(current_source_file < (data_source_files.size())){
string file_path = (this->data_source_files[this->current_source_file]);
string full_path = data_source_dir + file_path ;
buff_reader->open((char*)full_path.c_str());
}
else{
no_more_files = true;
}
return no_more_files; // let the caller know whether there was another file or not
}
And this is the method that does the parsing:
vector<string> TabDelimitedSource::getNext() {
// Returns the next document (a given cell) from the file(s)
string row; // Return NULL if no more documents/rows
vector<string> document;
try{
//Read each line in the file, corresponding to and individual document
std::getline(*buff_reader,row,'\n');
}
catch (ifstream::failure e){
; // Ignore and fall through
}
if (row.size()>0){
this->current_row += 1;
vector<string> cells;
this->split(row, "\t", cells); // Split the row on tabs
try{
string original_document = cells[column_holding_doc];
try{
split(original_document," ",document);
}catch (std::out_of_range e){
throw std::out_of_range ("Out of Range"); // ignore and fall through
}
}
catch (std::out_of_range e){
throw std::out_of_range ("Out of Range");
}
}
else{
// We're at the end of the current file, try loading the next one
buff_reader->close();
bool no_more_files = this->setup_next_buff_reader();
// If there was another file to load, recurse to get its first document
if (!no_more_files){
return this->getNext();
}
}
// Return our arrayList as an array... there has to be a better way to do this
vector<string> return_val ;
if(document.size()>0){ // return NULL by default
for(int i=0; i<(int)document.size(); i++){
return_val.push_back(document[i]);
}
}
return return_val;
}
Split Method:
void TabDelimitedSource::split(const string& str, const string& delim, vector<开发者_如何学编程;string>& result){
size_t start_pos = 0;
size_t match_pos;
size_t substr_length;
while((match_pos = str.find(delim, start_pos)) != string::npos){
substr_length = match_pos - start_pos;
if (substr_length > 0){
result.push_back(str.substr(start_pos, substr_length));
}
start_pos = match_pos + delim.length();
}
substr_length = str.length() - start_pos;
if (substr_length > 0){
result.push_back(str.substr(start_pos, substr_length));
}
}
Thanks in advance
Dave
You'll need to convert your UTF-8 file into UTF-16 (wstring) before you can do any of the parsing.
As you're using Windows, you can use MultiByteToWideChar to accomplish this
http://msdn.microsoft.com/en-us/library/dd319072(v=VS.85).aspx
There's some links to source code within.
精彩评论