Help with file handling

Hello m8s. I have one question that's bothering me.
How can I check if certain file exists?
For example, I want to write some default text to a file if prior to opening it didn't exist.
Thanks in advance.
[227 byte] By [AntonPodolsky] at [2007-11-20 10:56:21]
# 1 Re: Help with file handling
From C++ language point of view, you can only try to open the file and see if that works. If it doesn't however, there can be multiple reasons (file does not exist, permission denied, ...).

To really check if a file exists, you need to use OS specific functions. Unfortunately you do not tell in your post, what OS you are using, so I cannot help you further.
treuss at 2007-11-9 1:24:52 >
# 2 Re: Help with file handling
Unfortunately you do not tell in your post, what OS you are using, so I cannot help you further.
I'm using windows.
AntonPodolsky at 2007-11-9 1:25:48 >
# 3 Re: Help with file handling
Unfortunately theres no FileExists function or soemthing similar (at least none I know of). You can try to query its attributes and check if the operation fails. If it fails you have to check the error code against ERROR_FILE_NOT_FOUND to know why the call failed.

#include <windows.h>

bool FileExists( LPCTSTR strFileName ) // or std::string / CString
{
if( 0xffffffff == ::GetFileAttributes( strFileName ) )
{
// failure, determine why it failed
if( ERROR_FILE_NOT_FOUND == ::GetLastError() )
{
// file doesnt exist
return false;
}
}
return true;
}
GNiewerth at 2007-11-9 1:26:47 >
# 4 Re: Help with file handling
Thanks, it seems it'll do the job.
Strange though there is no built-in function for this.
One other way would be to check whether the file size is 0, but there's no function for telling the file size either. :(
AntonPodolsky at 2007-11-9 1:27:56 >
# 5 Re: Help with file handling
I'm using windows.
You can use FindFirstFile ( http://msdn2.microsoft.com/en-us/library/Aa364418.aspx) to see if a file exists. Upon success (file found), the function will also tell you the size of the file.
treuss at 2007-11-9 1:28:55 >
# 6 Re: Help with file handling
You can use FindFirstFile (http://msdn2.microsoft.com/en-us/library/Aa364418.aspx) to see if a file exists. Upon success (file found), the function will also tell you the size of the file.

I decided not to suggest FindFirstFile function because you have to close a handle afterwards (and may forget it). It may be the cleaner way, though.
GNiewerth at 2007-11-9 1:29:54 >
# 7 Re: Help with file handling
u can try this function _access(). this function takes 2 parameters file path and flag. flag is 0 for checking the existence of the file.
john_avi at 2007-11-9 1:30:52 >
# 8 Re: Help with file handling
Another alternative is to use boost::filesystem::exists().
Volantiz at 2007-11-9 1:31:55 >
# 9 Re: Help with file handling
Just curious... why use a windows-specific function when

std::ifstream testFile("file.txt");
if(testFile.fail()) /* File doesn't exist. */

will work just as well? Honestly, if you can't open it, it's as good as not there...

Otherwise, you can just open the file for read/write in append mode, then use the tellg method to determine the size of the file. Note that this will create the file if it doesn't exist, but the OP sounds like that's exactly what he wants to do.

Cheers,
Zen
zennehoy at 2007-11-9 1:32:55 >
# 10 Re: Help with file handling
Honestly, if you can't open it, it's as good as not there...NO, NO, NO. If you can't open it, that means nothing but "you can't open it". You might not have permissions to the file or the file might have been opened by a different process in exclusive mode.

Programs that tell "File not found.", when they should tell me "Permission denied" are very annoying!
treuss at 2007-11-9 1:33:54 >
# 11 Re: Help with file handling
How about "File not found or permission denied." :)
Alright, I'll admit that using tellg to determine if the file was originally empty is the better option, especially based on the OP's requirements.
Cheers,
Zen
zennehoy at 2007-11-9 1:35:03 >