Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: VC++
Hi all ,
I want to check if the file at given path exists and if it does then want to delete this it .
How can i do this .
Thnx all
Posted 4-Nov-12 23:25pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You can use _stat[^] to check whether the file exists or not. If it does, then remove[^] will delete the file.
Best regards
Espen Harlinn
Sergey Alexandrovich Kryukov at 19-Nov-12 18:43pm
Right, a 5.
Espen Harlinn at 19-Nov-12 18:46pm
Thank you, Sergey :-D
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

consider the following example
#include <windows.h>
#include <iostream>

using namespace std;
bool exists(char* filePath)
	//This will get the file attributes bitlist of the file
	DWORD fileAtt = GetFileAttributesA(filePath);
	//If an error occurred it will equal to INVALID_FILE_ATTRIBUTES
		//So lets throw an exception when an error has occurred
		throw GetLastError();
	//If the path referers to a directory it should also not exists.
	return ( ( fileAtt & FILE_ATTRIBUTE_DIRECTORY ) == 0 ); 
int main()
	if (exists("test.txt"))
		cout << "test.txt exists!\n";
		cout << "test.txt does not exists!\n";
	return 0;
use this code, you can ensuare file exist or not. if it is exist
you can use

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 528
1 OriginalGriff 459
2 ChintanShukla 305
3 Richard Deeming 275
4 RyanDev 230
0 Sergey Alexandrovich Kryukov 8,858
1 OriginalGriff 7,451
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893

Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100