Thread: Is there a file.DeleteFile() function

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    164

    Question Is there a file.DeleteFile() function

    Does anyone know of a simple function I can use to delete a file my program creates early on its life.

    I was hoping to find a simple DeleteFile () function that I could use similar to how I use : file.Open(), using fstream.h

    does anyone know if this exists, if so what header do I need, ect..

    Thanks for any help-

  2. #2
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Oh my goodness.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    remove() - ANSI
    DeleteFile() - Windows
    unlink() - *nix

    gg

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    164
    Thanks for the pointers! I looked into the DeleteFile() for windows. I see the concept of what it is saying and I will try to use that, I've tried grasping Microsofts web instructions, it just seems to read like stereo instructions, lacks the ability to follow....

    but that should give me the ideas of research I need, I appreicaite the response.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    164
    Well I attempted the Delete() function using msdn's examples and although I'm not seeing any compilation errors, the delete does not work. I know the file creat happens, the write to file is good, and then nada....

    ms does not have a clear example of the code, they show the createfile function in detail, but for the delete file function it just says to see the createfile, I used the createfile function and got it to work, but when attempting to later use the delete, again nothing...

    Code:
    BOOL DeleteFile(
      LPCTSTR lpFileName
    );
    I'm not familiar with using handles to files, and I think thats my problem, I've always just used the fstream methods and had no issues, do I just create a handle to the file and then call the delete function
    Code:
     handle hFile;
    hFile = "C:\\myfile";
    BOOL DeleteFile (LPCTSTR lphFile);
    Sorry for the newbie stuff, but this is driving me nuts!

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    DeleteFile() is a seperate function and you do not need to use CreateFile(). Just pass DeleteFile() the name of the file you wish to delete.
    Code:
    DeleteFile("C:\\myfile.txt");
    DeleteFile("A:\\virus\\doom.exe");
    DeleteFile("C:\\Program Files\\Browswer\\History.dat");
    --
    char * file = "C:\\SomeFileToDelete.del";
    DeleteFile(file);
    --
    Code:
    BOOL DeleteFile (LPCTSTR lphFile);
    This is a function prototype. A function prototype does not call the function.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    164
    I was hoping it would be that simple : -) but when I tried:
    Code:
    DeleteFile("C:\\myfile.txt");
    with my file name it still did not delete the file. I don't get any errors when compiling and executing the file simply does not get deleted. I checked my file locations and name and everything seems correct...

    am I missing something, here is the code:
    Code:
    	ifstream file; 
    	char x;
    	CString buffer;
    	//Open file 
    
    	file.open("C:\\mydbresults.txt", ios::in); //What ever your file is 
     
    
    	//Check for file exist 
    	if(!file) 
    	{ 
    		CString facts = "File not found"; 
    		MessageBox(facts,NULL,MB_OK); 
    	} 
    
    	//Read data 
    	while(file.get(x)) 
    	{ 
    
    		if(x=='\n') 
    		{ 
    			m_List1.AddString(buffer); 
    			//MessageBox(buffer,NULL,MB_OK); //Test buffer 
    			buffer=""; 
    		} 
    		else 
    		buffer = buffer + x; 
    	} 
    
    
    		m_List1.AddString(buffer); 
    		UpdateData(FALSE);
    	DeleteFile("C:\\mydbresults.txt");

    Thanks-

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Typically, you can't delete an open file. Call file.close() first.
    Code:
    	file.close();
    	DeleteFile("C:\\mydbresults.txt");
    If you're interested in debugging a windows function, you can call GetLastError().

    Code:
    	if (!DeleteFile("C:\\mydbresults.txt"))
    	{
    		cout << "DeleteFile() failed with error " << GetLastError() << endl;
    	}
    Last edited by anonytmouse; 06-22-2004 at 11:47 AM.

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    164
    I found my problem, on msdn of all places!!! : -) I finally saw the note that the file must be closed in order to delete.... Amazing what happens when you read the notes properly, sorry for that everyone, but thanks again for all your help!

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    164
    Thanks anonytmouse, you and codeplug were huge help. Very appreciated!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM