is there a way to rewrite only the first 30 characters of a text file using std::ofstream without clearing all the other contents of a text file? I looked in the web, but that didn't help. One guy offered to rewrite the entire file, but that's very unefficient (there are +900*30 other characters). Anyone? Oh, and here's my code:
void Unlock(int Level)
{
ifstream CheckIfExists("levels.txt");
if (!CheckIfExists.good())
{
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "ERROR",
"Unable to open the levels file. Next level won't be unlocked!", window);
CheckIfExists.close();
return;
}
CheckIfExists.close();
if(Level >= 0 && Level <= 29)
{
ofstream New_Locks("levels.txt");
Locked[Level] = UNLOCKED;
for(int i = 0; i < 30; ++i) New_Locks << Locked[i];
New_Locks.close();
return;
}
else SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Congratulations!",
"You won! Thank you for playing! Maybe once more?", window);
return;
}
use fstream
instead of ofstream
ifstream
is for reading only, ofstream
for writing only, fstream
does both.
Also note: it is not necessary to explicitly close the files. The fstream destructor will close them properly.