0x00B67361处的未处理异常

时间:2013-05-21 03:09:36

标签: c++ visual-studio visual-c++

我正在尝试将.dat文件读入2D数组,我尝试将相同的文件成功读入1D数组,这样每个数组的每一行。但是,使用下面的2D数组代码会弹出一个消息框,指出“ConsoleApplication11.exe中0x00B67361处的未处理异常:0xC0000005:访问冲突读取位置0x00000000”。并且没有完成执行“。未处理的异常背后的原因是什么?我使用VS 2012快递版。

do {
        char * s = find_data.cFileName;

        ifstream fin;
        fin.open(s); // open a file
        if (!fin.good()) 
            return 1; // exit if file not found

        // read each line of the file
        while (!fin.eof())
        {
            // read an entire line into memory
            char buf[MAX_CHARS_PER_LINE];
            int n = 0;
            int s = 0;
            int m = 0;

            // array to store memory addresses of the tokens in buf
            const char* token[MAX_TOKENS_PER_LINE][MAX_TOKENS_PER_LINE] = {}; // initialize to 0
            for (m = 1; m < MAX_TOKENS_PER_LINE; m++)
            {
                fin.getline(buf, MAX_CHARS_PER_LINE);

                // parse the line into blank-delimited tokens
                // a for-loop index
                //char* next_token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0
                char *next_token;
                // parse the line
                token[0][0] = strtok_s(buf, DELIMITER, &next_token); // first token
                //token[0] = strtok(buf, DELIMITER); // first token
                if (token[0][0]) // zero if line is blank
                {
                    for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
                    {
                        token[m][n] = strtok_s(0, DELIMITER, &next_token); // subsequent tokens
                        //token[n] = strtok(0, DELIMITER); // subsequent tokens
                        if (!token[m][n]) break; // no more tokens
                    }
                }
            }
            // process (print) the tokens
            for (int i = 0; i < n; i++) // n = #of tokens
                for (int j = 0; j < m; j++)
                {
                    cout << "Token[" << i << "," << j << "] = " << token[i][j] << endl;
                    cout << endl;

                }
        }
        // Your code here
    } while( FindNextFile( h, & find_data ) );
    FindClose( h );

1 个答案:

答案 0 :(得分:6)

问:0xC0000005:访问冲突读取位置0x00000000。未处理的例外背后的原因是什么?

答:您在代码中引用了一个空指针:)

建议:

单步执行MSVS调试器。每次strtok_s()返回“0”时要特别注意......并确保稍后不尝试访问该空指针。确保您实际上正在处理8位字符(谁知道:您的编译器设置可能会为您提供16位Unicode)。最重要的是:确定导致崩溃的确切行,并从该行正在处理的数据中向后工作。

MSVS有一个很好的调试器:你应该能够立即找到并纠正问题:)

祝你好运!

相关问题