调试器断言错误-字符串下标超出范围

时间:2020-01-27 21:42:32

标签: c++ string debugging

调试器出现问题,调试断言失败。它说这是由于字符串下标超出范围引起的。在第3469行中,该文件位于一个名为“ xstring”的文件中,我认为它在#include库之一中。我不确定代码中的什么会引起问题。

#include <iostream>
#include <fstream>
#include <string>
#include <assert.h>

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)

using namespace std;

char* extractTagC(char* a_txtLine, int txtLineSize, char openChar, char closeChar);
string extractTagCPP(string txtLine, char openChar, char closeChar);

int main()
{
    const int MAX_NUM_TOKENS = 10;
    // Dynamic arrays of cstrings and strings
    char** a_cStrings = new char* [MAX_NUM_TOKENS];
    string* a_strings = new string[MAX_NUM_TOKENS];

    const char* a_textFile = "TextFile.txt";

    // Read in the same text file twice using C syntax and C++ syntax
    // 1) Read in the file using the C way

    FILE* cFile;

    cFile = fopen("TextFile.txt", "r");
    assert(cFile != NULL);

    const int LINE_BUFFER_SIZE = 100;
    int numLinesC = 0;
    int numTagsC = 0;
    char a_txtBuffer[LINE_BUFFER_SIZE];
    // fgets returns NULL if at the end of the file.
    while (fgets(a_txtBuffer, LINE_BUFFER_SIZE, cFile))
    {
        char* tag = extractTagC(a_txtBuffer, LINE_BUFFER_SIZE, '<', '>');
        char* p_txtStart = a_txtBuffer;
        while (tag && numLinesC < 10)
        {
            a_cStrings[numTagsC] = tag;
            numTagsC++;
            p_txtStart = strchr(p_txtStart, '>');
            //Find the end of the tag to search for the next tag
            if (p_txtStart != NULL)
            {
                p_txtStart = p_txtStart + 1;
            }
            // We shouldn't have a tag without a closing bracket.
            // Get the next tag
            if (p_txtStart != NULL)
            {
                tag = extractTagC(p_txtStart, LINE_BUFFER_SIZE, '<', '>');
            }
            else if (p_txtStart == NULL)
            {
                break;
            }
        }
        numLinesC++;
    }
    fclose(cFile);

    // C++ File Reading
    ifstream fin;
    fin.open("TextFile.txt");

    int numLinesCPP = 0;
    int numTagsCPP = 0;
    string myLine = "";
    string tagText;
    while (getline(fin, myLine) && numLinesCPP < 10)
    {
        //Find the < and > tokens.  Both need to be on the one line
        tagText = extractTagCPP(myLine, '<', '>');
        string startTxt = myLine;
        while (!tagText.empty())
        {
            a_strings[numTagsCPP] = tagText;
            numTagsCPP++;
            // Move the text position for the next tag
            int closePos = startTxt.find('>', 0);
            startTxt = startTxt.substr(closePos + 1);
            tagText = extractTagCPP(startTxt, '<', '>');
        }
        numLinesCPP++;
    }
    fin.close();

    // Make sure you output the same things.
    assert(numTagsCPP == numTagsC);
    // 1) C output
    printf("The C tags are: ");
    for (int i = 0; i < numTagsC; i++)
    {
        printf("- %s ", a_cStrings[i]);
    }
    printf("\n");

    // 2) C++ output
    cout << "The C++ tags are: ";
    for (int i = 0; i < numTagsCPP; i++)
    {
        cout << " ++ " << a_strings[i];
    }
    cout << endl;

    // Clear the memory for each array element AS NEEDED
    delete[] a_cStrings;
    delete[] a_strings;
}

char* extractTagC(char* a_txtLine, int txtLineSize, char openChar, char closeChar)
{
    char* tagTxt = (char*)calloc(txtLineSize, sizeof(char*));
    bool tag = false;
    int pos = 0;

    for (int i = 0; i < txtLineSize; i++)
    {
        if (a_txtLine[i] == closeChar)
        {
            break;
        }

        if (tag == true)
        {
            tagTxt[pos] = a_txtLine[i];
            pos++;
        }

        if (a_txtLine[i] == openChar)
        {
            tag = true;
        }
    }
    return tagTxt;
}

string extractTagCPP(string txtLine, char openChar, char closeChar)
{
    int open = txtLine.find(openChar);
    if (open != txtLine[open])
    {
        int close = txtLine.find(closeChar, open + 1);
        if (close != txtLine[close])
        {
            open++;
            int length = close - open;
            return txtLine.substr(open, length);
        }
    }
    return "";
}

0 个答案:

没有答案