【问题标题】:Buffer overload Error(C++ Visual Studios)缓冲区过载错误(C++ Visual Studios)
【发布时间】:2015-07-24 08:57:00
【问题描述】:

这是我读取文件并从文件中的某个位置存储一些行并将其保存到 char 数组的代码。 如果我在主程序中调用 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] != '\0'; i++)
    {
        sWord_size++;
    }
    for (int i = 0; eWord[i] != '\0'; 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] = '\0'; //placing character array terminating character

        //this loop is searching for the word in the array
        for (int i = 0; array[i] != '\0'; i++)
        {
            for (int j = 0; sWord[j] != '\0' && j < 20 ; j++)
            {
                if (array[i] != sWord[j])
                {
                    break;
                }
                else
                {
                    i++;
                    if (sWord[j + 1] == '\0')
                    {
                        start = i-sWord_size+23;
                    }
                }
            }
        }
        for (int i = 0; array[i] != '\0'; i++)
        {
            for (int j = 0; eWord[j] != '\0' && j < 20 ; j++)
            {
                if (array[i] != eWord[j])
                {
                    break;
                }
                else
                {
                    i++;
                    if(eWord[j + 1] == '\0')
                    {
                        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;
}

【问题讨论】:

  • 您知道delete[] 运算符和正确的代码格式吗?
  • 您应该查找strlenstrstr。或者使用std::string,因为这是 C++。

标签: c++ file buffer overloading


【解决方案1】:

您的 char aConfig[] 是一个有 2 个索引的数组。第一个是空格,第二个是\0,表示数组的结束。

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

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

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

【讨论】:

    【解决方案2】:

    在主要

    char aConfig[] = " ";
    

    创建数组char[2](最后是空格和null)。第二个(bConfig)创建另一个数组char[2]

    在getVNP中

    outStr[i] = s[i];
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 2020-06-04
      • 2016-07-17
      • 1970-01-01
      • 2017-05-12
      • 2016-05-20
      相关资源
      最近更新 更多