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

时间:2015-07-24 08:57:00

标签: c++ file buffer overloading

这是我的代码,用于读取文件并从文件中的某个位置存储一些行并将其保存到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] != '\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;
}

2 个答案:

答案 0 :(得分:0)

主要

char aConfig[] = " ";

创建数组char[2](末尾为空格和null)。第二个(bConfig)创建另一个数组char[2]

在getVNP中

outStr[i] = s[i];

当您只有2个字符时,您正在写入此静态数组。

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

答案 1 :(得分:0)

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

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

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

如果您使用new分配内容,也应该进行清理。这不是java。