This is my code to read a file and store some lines from a position in the file and save it to an char array. The code works fine , if i call getVNP() once in the main. However when i try calling it the second time , I encounter a buffer overload error. I tried searching , but I haven found a solution please help thanks.
#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;
}
In main
char aConfig[] = " ";
creates array char[2]
(space and null at the end). The second one ( bConfig
) creates another array char[2]
.
In getVNP
outStr[i] = s[i];
You are writing to this static array while you have only place for 2 characters.
Condider changing aConfig and bConfig to std::string or allocating bigger buffer ( char aConfig[255];
).
Your char aConfig[]
is an array with 2 indices. The first one is the space and the second one \\0
to indicate the end of the array.
If your path is longer than 1 letter, you should either dynamically allocate this, pass the function a std::string by reference, or use the MAX_PATH
define (thought I recommend the std::string solution).
void getVNP(std::string fileName, std::string &outStr);
Also you should clean up if you allocate something with new
. This is not java.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.