how to find whether a file already exists in a folder?
-
Hi, This might sound a bit silly as this is not supposed to be a question in the VC++ forum. Nevertheless, I hope I can get an answer from somebody How to find whether a file already exists in a folder? Regards and thanks in advance Deepak Samuel
-
Hi, This might sound a bit silly as this is not supposed to be a question in the VC++ forum. Nevertheless, I hope I can get an answer from somebody How to find whether a file already exists in a folder? Regards and thanks in advance Deepak Samuel
-
Hi, This might sound a bit silly as this is not supposed to be a question in the VC++ forum. Nevertheless, I hope I can get an answer from somebody How to find whether a file already exists in a folder? Regards and thanks in advance Deepak Samuel
Open up "My Computer" and double-click the folders to the right directory... I take it you're asking about file existance from an
fstream
point of view. If you're looking if "foo.txt" exists you can do something like the following:ifstream fin( "foo.txt" );
if ( fin )
{
//You get in here the file's already there
}
else
{
//File's not created
}You could also use the ios flags
ios::nocreate
andios::noreplace
with anofstream
object:ofstream fout( "foo.txt" ios::noreplace )
if ( fout )
{
//File didn't exist but it does now
}
else
{
//File existed and your fout creation failed
}||
ofstream fout( "foo.txt" ios::nocreate )
if ( fout )
{
//File existed but it's about to get truncated
}
else
{
//File doesn't exist and your fout creation failed
}Hopefully that helped a little. Always Fear the Man with Nothing to Lose Jeryth
-
Hi, This might sound a bit silly as this is not supposed to be a question in the VC++ forum. Nevertheless, I hope I can get an answer from somebody How to find whether a file already exists in a folder? Regards and thanks in advance Deepak Samuel
From MSDN Library /* ACCESS.C: This example uses _access to check the * file named "ACCESS.C" to see if it exists and if * writing is allowed. */ #include #include #include void main( void ) { /* Check for existence */ if( (_access( "ACCESS.C", 0 )) != -1 ) { printf( "File ACCESS.C exists\n" ); /* Check for write permission */ if( (_access( "ACCESS.C", 2 )) != -1 ) printf( "File ACCESS.C has write permission\n" ); } } http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/\_crt\_\_access.2c\_.\_waccess.asp