首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用_tcstok时的访问冲突

使用_tcstok时的访问冲突
EN

Stack Overflow用户
提问于 2011-07-13 00:02:13
回答 1查看 726关注 0票数 0

我正在尝试使用_tcstok对文件中的行进行标记。我可以将行标记一次,但是当我第二次尝试标记它时,我得到了访问冲突。我觉得这实际上并不是在访问这些值,而是与位置有关。不过,我不确定还能怎么做。

谢谢,

戴夫

附注:我使用了TCHAR和_tcstok,因为这个文件是UTF-8。

这是我得到的错误:

Testing.exe: 0xC0000005中0x63e866b4 (msvcr90d.dll)处的第一次机会异常:访问冲突读取位置0x0000006c。

代码语言:javascript
代码运行次数:0
运行
复制
vector<TCHAR> TabDelimitedSource::getNext() {
// Returns the next document (a given cell) from the file(s)
TCHAR row[256]; // Return NULL if no more documents/rows
vector<TCHAR> document;

try{
    //Read each line in the file, corresponding to and individual document
    buff_reader->getline(row,10000);
    }
catch (ifstream::failure e){
        ; // Ignore and fall through
    }

if (_tcslen(row)>0){
    this->current_row += 1;
    vector<TCHAR> cells;
      //Separate the line on tabs (id 'tab' document title 'tab' document body)
     TCHAR *  pch;
     pch = _tcstok(row,"\t");
     while (pch != NULL){
         cells.push_back(*pch);
         pch = _tcstok(NULL, "\t");
     }

    // Split the cell into individual words using the lucene analyzer
    try{
      //Separate the body by spaces
        TCHAR original_document ;
        original_document = (cells[column_holding_doc]);
        try{
            TCHAR * pc;
            pc = _tcstok((char*)original_document," ");
             while (pch != NULL){
                 document.push_back(*pc);
                pc = _tcstok(NULL, "\t");
             }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-13 00:27:57

首先,您的代码是C字符串操作和C++容器的混合。这只会把你挖进洞里。理想情况下,您应该将该行标记为std::vector<std::wstring>

此外,您对TCHAR和UTF-8也很困惑。TCHAR是一种根据编译时间标志在8到16位之间“浮动”的字符类型。UTF-8文件使用一个到四个字节来表示每个字符。因此,您可能希望将文本作为std::wstring对象保存,但需要显式地将UTF-8转换为wstring。

但是,如果你只是想让任何东西工作,专注于你的标记化。您需要存储每个令牌的起始地址(作为TCHAR*),但是您的向量是TCHARs的向量。当您尝试使用令牌数据时,您是在将TCHAR转换为TCHAR*指针,从而导致访问冲突,这并不令人意外。您提供的AV地址是0x0000006c,这是字符l的ASCII码。

代码语言:javascript
代码运行次数:0
运行
复制
  vector<TCHAR*> cells;
  ...
  cells.push_back(pch);

..。然后..。

代码语言:javascript
代码运行次数:0
运行
复制
    TCHAR *original_document = cells[column_holding_doc];
    TCHAR *pc = _tcstok(original_document," ");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6667326

复制
相关文章

相似问题

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