0

I am trying to read .dat files into 2D array, I tried successfully read same files into 1D arrays such that each line for each array. However, with the code below for 2D array a message box pop up stating " Unhandled exception at 0x00B67361 in ConsoleApplication11.exe: 0xC0000005: Access violation reading location 0x00000000." and does not finish the execution". What's the reason behind the unhandled exception? I use VS 2012 express edition.

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 );
4

1 回答 1

6

Q: 0xC0000005: Access violation reading location 0x00000000. What's the reason behind the unhandled exception?

A: You're reference a null pointer in your code :)

SUGGESTION:

Single-step through the MSVS debugger. Pay careful attention every time strtok_s() returns "0" ... and make sure you don't try to access that null pointer later. Make sure you're actually processing 8-bit characters (who knows: your compiler settings might be giving you 16-bit Unicode). Most important: identify the exact line that's causing the crash, and work backward from the data that line is processing.

MSVS has a great debugger: you should be able to find - and correct - the problem in no time :)

Good luck!

于 2013-05-21T03:14:01.747 回答