Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to provide the user with a warning and a way out if he mistakenly tries to open an exist file with ios::out.

Here is the code I am using to open a new file or wipe an old one clean and begin writing new data.


ofstream output(szSaveName,ios::app);
if(!output)
{
cerr << "Could not open " << szSaveName << endl;
if (cin.peek())
return 0;
}
I lifted this snippet from somewhere a couple of years and didn't anticipate the problems it could cause. People expect to be warned before wiping out data.
Posted
Updated 16-Feb-15 5:23am
v2
Comments
BacchusBeale 15-Feb-15 20:36pm    
look here http://stackoverflow.com/questions/12774207/fastest-way-to-check-if-a-file-exist-using-standard-c-c11-c
Sir Roland 16-Feb-15 11:33am    
Thanks, Baccus. I pasted test0 into my code but Visual studio 2010 didn't accept inline although it did flag it as a reserve word. I installed the .h file called for in the snippet. I'm have never used inline. Is there a C language test that will do the job? Thanks again

Start by using a dialog or console message to ask the user for the name of the file they wish to write. Then check if the file exists, and if so ask them if they wish to overwrite it.
 
Share this answer
 
Comments
Sir Roland 16-Feb-15 11:11am    
Thanks for the comment. I have the coding to get the name. What I'm looking for is how to find if the file already exists.
The Windows GetOpenFileName and GetCloseFileName structures have a wonderful selection of flags that make saving a file a well guarded operation, if properly implemented. I had completely forgotten them. Thanks to those who offered advice.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900