void Document::loadFile(string iFileExt)
{
ioFile = new fstream(iFileExt.c_str(), ios::in); // does this ever get deleted?
// string *content; // arrays have their place, and this is not it
std::vector<string> content;
if (ioFile->fail()){
cerr << "File failed to open for read" << endl;
exit(69); // don't ever just unconditionally exit from random places
// it makes for unmaintainable spaghetti code
}
for (int i = 0; ioFile->good(); ++i) // loop manages i for us
{
content.push_back((string()));
getline (*ioFile, content[i]);
}
ioFile->close();
}
The following might be useful when replacing the existing code, which uses an array of pointers, to something that takes a vector.
The function probably has a signature something like this:
void ProcessLinesInFile(int numLines, std::string *lines[]); // maybe this
void ProcessLinesInFile(int numLines, std::string **lines); // or maybe this
Both ways are essentially equivalent.
ProcessLinesInFile probably has a body something like this:
for (int i = 0; i < numLines; ++i)
{
*lines[i] = Process(*lines[i]); // read and maybe write the line
}
The first step would be to make it work with an array of strings, rather than an array of string pointers:
void ProcessLinesInFile(int numLines, std::string lines[])
{
// should behave exactly the same way as before
for (int i = 0; i < numLines; ++i)
{
lines[i] = Process(lines[i]); // read and maybe write the line
}
}
From there, using a vector is easy:
void ProcessLinesInFile(std::vector<std::string> &lines)
{
// should behave exactly the same way as before
for (int i = 0; i < lines.size(); ++i)
{
lines[i] = Process(lines[i]); // read and maybe write the line
}
}
If you're sure that you never need to actually change the array throughout this process, you can (and it is prudent to) pass the vector reference as a constant:
void ProcessLinesInFile(std::vector<std::string> const &lines);