What confuses me is that there is another function in the same file that opens the file successfully every time but this one fails. Bad flag is not set. Only fail flag is.
Can i somehow find out what is causing the problem?
Make sure the file isn't open somewhere else for writing.
By the way, don't use .open() and .close() - instead, let RAII handle opening and closing the file for you. Explicitly calling those member functions is only useful in special circumstances - doing it in other cases leaves extra room for human error.