我正在尝试使用_tcstok对文件中的行进行标记。我可以将行标记一次,但是当我第二次尝试标记它时,我得到了访问冲突。我觉得这实际上并不是在访问这些值,而是与位置有关。不过,我不确定还能怎么做。
谢谢,
戴夫
附注:我使用了TCHAR和_tcstok,因为这个文件是UTF-8。
这是我得到的错误:
Testing.exe: 0xC0000005中0x63e866b4 (msvcr90d.dll)处的第一次机会异常:访问冲突读取位置0x0000006c。
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");
}
发布于 2011-07-12 16:27:57
首先,您的代码是C字符串操作和C++容器的混合。这只会把你挖进洞里。理想情况下,您应该将该行标记为std::vector<std::wstring>
此外,您对TCHAR
和UTF-8也很困惑。TCHAR
是一种根据编译时间标志在8到16位之间“浮动”的字符类型。UTF-8文件使用一个到四个字节来表示每个字符。因此,您可能希望将文本作为std::wstring
对象保存,但需要显式地将UTF-8转换为wstring。
但是,如果你只是想让任何东西工作,专注于你的标记化。您需要存储每个令牌的起始地址(作为TCHAR*
),但是您的向量是TCHAR
s的向量。当您尝试使用令牌数据时,您是在将TCHAR
转换为TCHAR*
指针,从而导致访问冲突,这并不令人意外。您提供的AV地址是0x0000006c
,这是字符l
的ASCII码。
vector<TCHAR*> cells;
...
cells.push_back(pch);
..。然后..。
TCHAR *original_document = cells[column_holding_doc];
TCHAR *pc = _tcstok(original_document," ");
https://stackoverflow.com/questions/6667326
复制相似问题