Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check to see if a file exists

Status
Not open for further replies.

Czar24

Technical User
Mar 22, 2004
16
US
Hey all, I'm writing a program which saves the output to a single file like "output.txt"

I've been playing with the ios::nocreate and ios::noreplace but I can't get it to quite do what I want it to do (and yes I've included the #include <iostream> to activate them.

What I'm trying to do is this:

I want the program to save a given output as "output.txt"
If on subsequent runs, when the file is created again I want it to call the file "output1.txt" or "output2.txt" or whatever so that it never overwrites a previous output.

I've done something like this using a NAWK script with a simple for loop and a function while would return a 0 or 1 for the state if a given file exists, but I need this one to be in the executable and I haven't found the function.

Can anyone point me in the right direction?


Thanks!



Czar24
 
You could use pretty much the same idea.
Open the file for reading (doesn't destroy files, and doesn't create them). If the open succeeds, close it and try the next file in the sequence.

When you find a filename you can't open for reading, open it for writing.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top