Advertisement Jump to content
Sign in to follow this  
BaneTrapper

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

This topic is 1863 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Edited by rip-off

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!