2011-07-12 64 views
0

我想用_tcstok標記文件中的行。我能夠將該行標記爲一次,但是當我嘗試第二次對它進行標記時,就會出現訪問衝突。我覺得這跟沒有實際訪問這些值有關,而是取而代之。我不知道如何做到這一點,但。訪問衝突,同時使用_tcstok

感謝,

戴夫

附:我使用TCHAR和_tcstok,因爲該文件是UTF-8。

這是我得到的錯誤:在Testing.exe

在0x63e866b4(msvcr90d.dll)第一次機會異常:0000005:訪問衝突讀取位置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"); 
      } 
+0

你爲什麼鑄造'字符*'而不是'TCHAR *'?另外,你將'TCHAR'當作'TCHAR *'對待,這肯定是不好的。 –

+0

我擺脫了一些鑄造等。仍是同樣的問題。 – David

+0

明目張膽的不是。寫下它沒有演員,你應該能夠找到錯誤。 –

回答

1

首先,您的代碼是C字符串操作和C++容器的雜種混合物。這隻會把你挖到一個洞裏。理想情況下,您應該將該行標記爲std::vector<std::wstring>

另外,您對TCHAR和UTF-8很困惑。 TCHAR是一種字符類型,它根據編譯時間標誌在8位和16位之間「浮動」。 UTF-8文件使用1到4個字節來表示每個字符。因此,您可能希望將文本保存爲std::wstring對象,但您需要將UTF-8顯式轉換爲wstrings。

但是,如果您只想獲得任何東西的工作,請關注您的標記化。您需要存儲每個令牌開始的地址(作爲TCHAR*),但您的向量是代替TCHAR的向量。當您嘗試使用令牌數據時,您正在將TCHAR s指向TCHAR*指針,並且出現訪問衝突的不出所料。您提供的AV地址是0x0000006c,這是字符l的ASCII碼。

vector<TCHAR*> cells; 
    ... 
    cells.push_back(pch); 

...然後...

TCHAR *original_document = cells[column_holding_doc]; 
    TCHAR *pc = _tcstok(original_document," "); 
+0

謝謝。這是我第一次需要處理UTF-8文件,這是我的想法。這比我覺得應該更復雜。 – David

+0

非常感謝。我知道這實際上並沒有解決它,但很高興看到事情正在進行,以便我知道其他一切正常工作。我一定會開始努力把所有東西都轉移到wstrings。 – David