Multiple problems here:
Firstly, your return
statement is inside your while
loop, which means the loop will only execute once and then you'll just return from the function. The return
statement needs to be placed outside of the while
loop.
Secondly, you don't allocate any storage for Buffer
. All you do is declare a char*
pointer, which isn't even initialized. You need to explicitly allocate storage so that ifstream::getline
has somewhere to store the input. You can use new
to allocate storage, but since your Read
function returns a char*
pointer, the calling function would need to manually manage the memory, which is generally just a headache.
A better solution is to use the free function std::getline
, since it takes an std::string
, so you don't have to worry about storage allocation. You'll also need to change the function signature of Read
to return an std::string
.
std::string Read(std::string FilePath)
{
std::ifstream F_S(FilePath.c_str());
/* You should also check for `F_S.good()` here */
std::string result, Buffer;
while(std::getline(F_S, Buffer))
{
result += Buffer;
}
return result;
}
Note that if you want to preserve carriage returns in the file, you'll need to manually add them to result
within each iteration, since std::getline
discards delimiter characters.