Saving files with std::ofstream checking if filename alredy exists to prompt "Are you sure you want to overwrite"

Started by
2 comments, last by BaneTrapper 10 years, 4 months ago

EDIT::
Sorry for post i sould google after first page, found it imedietelly.

Hello.

Currently i am making mapeditor.

At point where a user presses "Save map" he is prompted to enter a name for the map.

After he is done entering the map name i want to check if the filename alredy exists, in case it alredy exists i want to prompt to user a overwrite command that there is a file with that name already, or allow him to retype the name.

The issue is i do not know how to check if file exists i only know how to open and write to it.


//FOLDER with maps
1.txt
2.txt
iMadeThisMap.txt
...
//Code
void Map::SaveMap(int mapID)
{
    const std::string fileName(glob::MAP_MAP_DIRECTORY + std::to_string(mapID) + ".txt");
    //CHECK IF FILE EXISTS HERE
    std::ofstream OFStream(fileName);
    if(!OFStream.is_open())
    {
        glob::PrintError("Error at Map::Save error opening map file to save map, file issued to open: " + fileName);
        //OR CHECK IF FILE EXISTS HERE
        ...
        return;
    }
}

For solution:
http://stackoverflow.com/questions/4316442/stdofstream-check-if-file-exists-before-writing

Advertisement

Using Google to search for an answer is much shorter. There you go.

Please do not mark posts as "solved".

Please do not mark posts as "solved".

Thank you for doing it instead of me.

Can you link me to a page with rules or so, i do not want to make accidents like this.

Tried searching for it and googling something like "gamedev.net topic rules" on my end gave me just topics unrelated to rules.

This topic is closed to new replies.

Advertisement