I am reading strings in C++ using fread
where I am reading and storing shortSiteText
in siteNames_. siteNames_ is declared as std::vector<char*> siteNames_;
I use siteNames_ in other functions but because shortSiteText
is a pointer, when I call the delete command on it, the last entry in siteNames_
is changed. How do I prevent this?
for (unsigned int i = 0; i <= nSites; i ++){
fread((char *) &shortSiteTextLength, 1, sizeof shortSiteTextLength, baseFile_);
shortSiteText = new char[shortSiteTextLength];
fread(shortSiteText,1, shortSiteTextLength,baseFile_);
siteNames_.push_back(shortSiteText);
}
delete [] shortSiteText;
I tried to use the dereference operator: siteNames_.push_back(*shortSiteText);
but that generates a compiler error.
NOTE: I must use fread and char* due to legacy code.