So I have a file that has contents that need to be stored into a growing object. By growing I mean that it'll explicitly concatenate the new line it reads into the growing object, pBuff, each time every line is read from the file. I only get the last line to be outputted when I try to output the growing object after the file has been read (for my example below, only "Line 5.\n" is what gets output). Can someone steer me into the right direction?
Here is my code.
bool DynString::readLine(std::istream& in)
{
if(in.eof())
{
*this = DynString(); // Default string value.
pBuff = "";
return(false);
}
char s[1001];
in.getline(s, 1001);
// Delete old string-value and create new pBuff string with copy of s
delete [] pBuff;
pBuff = new char[1001];
strncpy(pBuff, s, 1001);
return(true);
}
// Read entire input file into string
bool DynString::readFile(const char filename[])
{
std::ifstream in(filename);
if(! in.is_open() )
{
*this = DynString(); // Default string value.
pBuff = "";
return(false);
}
// Delete old string-value and
// Read the file-contents into a new pBuff string
delete [] pBuff;
pBuff = new char[1001];
while(!in.eof())
{
readLine(in);
}
return(true);
}
Text file has contents like so:
Line 1.\n
Line 2.\n
Line 3.\n
Line 4.\n
Line 5.\n
The comments are what my professor put in as instructions so I DO have to follow them. I've been at this for hours and I'm still stuck! Any suggestions would be great! Also, I tried using the strcat function like so: strcat(pBuff, s); but that resulted in my program to crash :(