I am really stumped with a problem my program keeps giving me. Randomly, a file I try to open returns fail() to ifstream and can't be read from. I have a feeling it has to do with these 2 functions I have been using.
Please help.
Thanks!
PS: I tried boost filesystem to get the file size and I still get the same problem.
//Moves the file to 'fileS'. 0 returned if succesful
int FileToString(const string &file_path, string &fileS)
{
ifstream input(file_path.c_str(), ios::binary);
if (input.fail())
{
return -1;
}
long size = GetFileSize(input);
char* buffer = new char[size];
input.read(buffer, size);
fileS.assign(buffer, size);
delete buffer;
input.close();
return 0;
}
long GetFileSize(ifstream &file)
{
long temp = file.tellg();
file.seekg(0L, ios::end);
long end = file.tellg();
file.seekg(temp, ios::beg);
return end;
}