I'm trying to write a function that when called, will search my text file for a match to the parameter, then "update" it by overwriting the file.
here is my current function:
EDIT: Here is the new updated code, still not working :/
Here is the line it sends to UpdateGem: (Example sTempTxt: "AB:5.55")
ostringstream stream;
stream << std::setprecision(3) << fEindgem;
string sTempTxt = sVak + ":" + stream.str() + "\n";
UpdateGem(sTempTxt);
void UpdateGem(string text)
{
ifstream f;
f.open("GEM.txt");
string sGEMS[100];
string temp[3];
splitstring(text, ":", temp[0], temp[1]);
bool OverWrite;
int count = 0;
string Delete, line;
while(true)
{
getline(f, sGEMS[count], '\n');
if(f.eof()) break;
splitstring(sGEMS[count], ":", temp[1], temp[2]);
if (temp[0] == temp[1])
{
OverWrite = 1;
Delete = sGEMS[count];
}
count++;
}
// Don't set count to 0, since we need it
// count = 0;
ofstream f2;
f2.open("GEM2.txt"/*,std::ios_base::app*/);
if (OverWrite)
{
f.seekg(0, std::ios::beg);
for (int i = 0; i < count; ++i)
{
if (sGEMS[i] != Delete) f2 << sGEMS[i];
}
}
f.close();
f2.close();
remove("GEM.txt");
rename("GEM2.txt", "GEM.txt");
ofstream file;
file.open("GEM.txt",std::ios_base::app);
file << text;
file.close();
}
The line it has to replace takes the form of NAME:NUMBER, in which the number can be different, so I am using a splitstring function to compare the name to the name of the line found, then completely erasing that line and "updating" it by re-adding it later. However, my current code only writes the updated line to the file, not the old ones...