首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >读取UTF-8文件

读取UTF-8文件
EN

Stack Overflow用户
提问于 2011-07-08 23:48:45
回答 1查看 1.1K关注 0票数 3

我使用的是VS2008,需要读取具有UTF-8中文字符的文本文件。文件的组织方式如下:每行包含一个文档,文档以制表符分隔(index ' tab‘doc title 'tab’doc body)。因此,我需要做的是分隔选项卡上的行,然后用空格分隔第三列(文档正文),并将每个单词存储在一个向量中。当文件是ANSI编码时,所有这些都能正常工作。但是当它是UTF-8时,它抛出和断言失败(未签名)(c+1)<=256。我想保留当前的功能和流程,并尽可能少地使用第三方库。

我研究过不同的方法(ustream、wstream等)。但我对如何实际使用它们感到有点困惑。

下面是读入文件的方法:

代码语言:javascript
运行
复制
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
} 

下面是进行解析的方法:

代码语言:javascript
运行
复制
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方法:

代码语言:javascript
运行
复制
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));
}

}

提前感谢

戴夫

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-09 00:30:22

在进行任何解析之前,您需要将UTF-8文件转换为UTF-16 (wstring)。

当您使用Windows时,您可以使用MultiByteToWideChar来完成此操作

http://msdn.microsoft.com/en-us/library/dd319072(v=VS.85).aspx

里面有一些指向源代码的链接。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6626992

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档