读取文件(内存映射),然后将字符串拆分为令牌

时间:2014-07-29 13:53:46

标签: c++ string token

我遇到Visual Studio不断报告此代码的问题:

  

Z Final Project.exe中0x628D692E(msvcr120.dll)的未处理异常:0xC0000005:访问冲突写入位置0x00990004。

从文件中读取字符串后,我尝试将其拆分为令牌:

int main()
{
    HANDLE hFile , MapHandle;
    char * name = new char[20];
    char * word = new char[100];
    char * data , * MapPointer;
    strcpy_s(name , 10 , "d0000.txt");
    //List DocumentList;
    Trie document;
    clock_t start = clock();
    do
    {
        next_document(name , 4);
        hFile = CreateFileA(name , GENERIC_READ | GENERIC_WRITE , 0 , NULL , OPEN_EXISTING , FILE_FLAG_RANDOM_ACCESS , NULL);
        MapHandle = CreateFileMapping(hFile , NULL , PAGE_READWRITE | SEC_RESERVE , 0 , 0 , 0);
        if (MapHandle == NULL)
        {
            CloseHandle(hFile);
            break;
        }
        cout << name << ": ";
        MapPointer = (char *) (MapViewOfFile(MapHandle , FILE_MAP_READ , 0 , 0 , 0));
        start = clock();
        SplitText(MapPointer , document);
        cout << clock() - start << endl;
        UnmapViewOfFile(MapPointer);
        CloseHandle(MapHandle);
        CloseHandle(hFile); 
    } while (true);
    std::cout << clock() - start << std::endl;
    delete[] name;
    delete[] word;
    return 0;
}

void SplitText(char * text ,
               Trie & document)
{
    static char * delimiters = " ,.?\xc4\x88\x97\x93\x94\x60\xa8\xe1\xb4\"\t:;!()!@#$%^&*";
    char * token = nullptr , * next = nullptr;
    token = strtok_s(text , delimiters , &next);
    while (token != nullptr)
    {
        document.insert(token);
        token = strtok_s(nullptr , delimiters , &next);
    }
}

这里有什么问题?

0 个答案:

没有答案