Check if file exists

C

coolchap

Hi,
My requirement is to create a new text file as an output of a C+
+ program (for eg Output.txt), if the Output.txt file already exists,
then the data needs to be appended to the existing file.

Is there a way to check if a file exists before opening the file with
ofstream ?

I have seen that we can append using outputFile.open("filename.dat",
ios::app), but am not so sure about checking if the file exists before
writing into it.

Regards,
Prashant
 
V

Victor Bazarov

My requirement is to create a new text file as an output of a C+
+ program (for eg Output.txt), if the Output.txt file already exists,
then the data needs to be appended to the existing file.

Is there a way to check if a file exists before opening the file with
ofstream ?

If there is, it's most likely platform-specific. C++ only deals with
"streams".
I have seen that we can append using outputFile.open("filename.dat",
ios::app), but am not so sure about checking if the file exists before
writing into it.

Why bother? If the file exists, you're going to be appending to it, if
it doesn't, you will create a new file. Isn't that what you need?

V
 
C

coolchap

If there is, it's most likely platform-specific.  C++ only deals with
"streams".


Why bother?  If the file exists, you're going to be appending to it, if
it doesn't, you will create a new file.  Isn't that what you need?

V

Thanks for the reply.

Yes when the file exists, I need to count the number of lines in the
existing file and then append the new lines. Hence I need a condition
to find the existence of the file before writing into it.
 
C

coolchap

If there is, it's most likely platform-specific.  C++ only deals with
"streams".


Why bother?  If the file exists, you're going to be appending to it, if
it doesn't, you will create a new file.  Isn't that what you need?

V

Thanks for the reply

I would also like to detect the presence of file in order to count the
number of line before appending anything to the file
 
G

Geoff

Thanks for the reply.

Yes when the file exists, I need to count the number of lines in the
existing file and then append the new lines. Hence I need a condition
to find the existence of the file before writing into it.

How about:

istream ifile( "output.txt" );
if ( ifile.fail() )
// The file does not exist ...
 
J

James Kanze

Note that on most systems, this is, strictly speaking,
impossible. (Suppose you can't access one of the directories in
the path leading to the file.) I'm supposing you don't mean it
literally, but are concerned with a specific use.
Yes when the file exists, I need to count the number of lines
in the existing file and then append the new lines. Hence
I need a condition to find the existence of the file before
writing into it.

So open the file for reading, and read using getline.
Initialize your counter of lines to 0, and you don't even have
to test whether the open succeeded; just read until a read
fails. Then close the file, and reopen it in append mode for
writing.

No need for any special functions here.
 
J

Jeff Flinn

coolchap said:
Hi,
My requirement is to create a new text file as an output of a C+
+ program (for eg Output.txt), if the Output.txt file already exists,
then the data needs to be appended to the existing file.

Is there a way to check if a file exists before opening the file with
ofstream ?

I have seen that we can append using outputFile.open("filename.dat",
ios::app), but am not so sure about checking if the file exists before
writing into it.

See
http://www.boost.org/doc/libs/1_44_0/libs/filesystem/v3/doc/reference.html#exists
for a portable solution.

Jeff
 
V

Victor Bazarov

Even that can fail if not enough privileges (at least on *nux systems).

Uh... Sure, and the inability to open for reading would prevent the OP
from being able to count lines in the file, so for the intents and
purposes of the OP's algorithm the file doesn't exist. Of course,
opening that file for write might fail in that situation as well (and
likely). The OP might want to know how to overcome obstacles
potentially imposed by the system, but the answers to that are hardly on
topic in a language newsgroup. He needs to consult with the OS
newsgroup then, IMNSHO.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top