Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6103295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:42:45+00:00 2026-05-23T13:42:45+00:00

I’m using VS 2008 and need to read text files that have UTF-8 Chinese

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:42:46+00:00Added an answer on May 23, 2026 at 1:42 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of posts stored in text files formatted in yaml/textile (from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need to clean up various Word 'smart' characters in user input, including but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.