缓冲区过载错误(C++ Visual Studios)

Buffer overload Error(C++ Visual Studios)

这是我的代码,用于读取文件并从文件中的某个位置存储一些行并将其保存到 char 数组。 如果我在 main 中调用一次 getVNP() ,代码工作正常。 但是,当我第二次尝试调用它时,遇到缓冲区过载错误。 我尝试搜索,但我没有找到解决方案,请帮助谢谢。

#include <iostream> // library that contain basic input/output functions
#include <fstream>  // library that contains file input/output functions
#include <string>
using namespace std;

void getVNP(std::string fileName, char* outStr)
{
    int end;
    int start;
    char sWord[] = "Virtual";  
    char eWord[] = "[Default";
    int position = 0; //this will be used incremental to fill characters in the array 
    int sWord_size = 0;
    int eWord_size = 0;
    //this loop is calculating the length of input word
    for (int i = 0; sWord[i] != '[=10=]'; i++)
    {
        sWord_size++;
    }
    for (int i = 0; eWord[i] != '[=10=]'; i++)
    {
        eWord_size++;
    }

    fstream fin(fileName.c_str());
    fin.seekg(0, fin.end);
    int epos = fin.tellg();
    fin.seekg(0, fin.beg);
    cout << epos;
    int array_size = epos; // define the size of character array
    char * array = new char[array_size]; // allocating size an array 

    //opening an input stream for file test.txt
    /*checking whether file could be opened or not. If file does not exist or don't have read permissions, file
    stream could not be opened.*/
    if (fin.is_open())
    {
        //file opened successfully so we are here
        cout << "File Opened successfully!!!. Reading data from file into array" << endl;
        //this loop run until end of file (eof) does not occur
        while(!fin.eof() && position < array_size)
        {
            fin.get(array[position]); //reading one character from file to array
            position++;
        }
        array[position - 1] = '[=10=]'; //placing character array terminating character

        //this loop is searching for the word in the array
        for (int i = 0; array[i] != '[=10=]'; i++)
        {
            for (int j = 0; sWord[j] != '[=10=]' && j < 20 ; j++)
            {
                if (array[i] != sWord[j])
                {
                    break;
                }
                else
                {
                    i++;
                    if (sWord[j + 1] == '[=10=]')
                    {
                        start = i-sWord_size+23;
                    }
                }
            }
        }
        for (int i = 0; array[i] != '[=10=]'; i++)
        {
            for (int j = 0; eWord[j] != '[=10=]' && j < 20 ; j++)
            {
                if (array[i] != eWord[j])
                {
                    break;
                }
                else
                {
                    i++;
                    if(eWord[j + 1] == '[=10=]')
                    {
                        end = i-eWord_size - 11;
                    }
                }
            }
        }
        //take the start pos and the end pos text and put in string array s;
        fin.seekg(start);
        char *s = new char[end - start + 1];
        fin.read(s, end - start);
        s[end - start] = 0;
        size_t len = strlen(s);

        for(int i=0; i <len; ++i)
        {
            outStr[i] = s[i];
        }
        fin.close();
    }
    else //file could not be opened
    {
        cout << "File could not be opened." << endl;
    }
    getchar();
}

int main()
{
    std::string FilePath;
    std::string FilePath2;
    char aConfig[] = " ";
    char bConfig[] = " ";

    std::cout << "Please enter a file path: ";
    std::cin >> FilePath;
    std::cout << "Please enter a second file path: ";
    std::cin >> FilePath2;
    getVNP(FilePath, aConfig);
    cout << aConfig;
    getVNP(FilePath2, bConfig);
    cout << bConfig;

    getchar();
    return 0;
}

主要

char aConfig[] = " ";

创建数组 char[2] (space 和最后的 null)。第二个 (bConfig) 创建另一个数组 char[2].

在getVNP中

outStr[i] = s[i];

您正在写入此静态数组,而您只有 2 个字符的空间。

考虑将 aConfig 和 bConfig 更改为 std::string 或分配更大的缓冲区 (char aConfig[255];)。

您的 char aConfig[] 是一个包含 2 个索引的数组。第一个是space,第二个是[=12=],表示数组结束。

如果你的路径长于 1 个字母,你应该动态分配它,通过引用传递函数 a std::string,或者使用 MAX_PATH 定义(虽然我推荐 std::string解决方案)。

void getVNP(std::string fileName, std::string &outStr);

如果你用 new 分配了一些东西,你也应该清理。这不是 java.