C++ -- Creating file but unable to delete it

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 3
Reputation: mario20055 is an unknown quantity at this point 
Solved Threads: 0
mario20055 mario20055 is offline Offline
Newbie Poster

C++ -- Creating file but unable to delete it

 
0
  #1
Jul 2nd, 2009
HI Team,

First of all, I'm new to C++, so my knowlege is like 2% and I would like to thank you for any help provided. Well I'm working on a project that looks like this:
-- I'm creating an executalble (EXE) that will monitor and create a file into C:/program files/GMLocal7/ directory if it doesn't exist.
-- If the file gets deleted by user or another program, I want my exe to re-create it.
-- My EXE needs be running in the background w/o user interaction.

so far my current exe is creating the file but locks it so, when I try to delete the file Windows is telling me that the file is begin used by another program.

  1. Note: I don't want my EXE to delete the file..... The USER will be deleting this file.
  2.  

Does anyone know what needs to be added to the code in order for EXE not to lock the file its generating?


here's the code I'm using to compile my small app:
  1. #include "stdafx.h"
  2. #include "windows.h"
  3. #include "iostream"
  4. #include "fstream"
  5.  
  6. using namespace std;
  7.  
  8. int _tmain(int argc, _TCHAR* argv[])
  9. {
  10. HWND hWnd = GetConsoleWindow();
  11. ShowWindow( hWnd, SW_HIDE ); // hide the console window
  12.  
  13. while( true )
  14. {
  15. fstream file;
  16. file.open( "C:/Program Files/GMLocal7/.g0ldm1n3", ios::in );
  17.  
  18. if( file.is_open() == false )
  19. {
  20. file.open( "C:/Program Files/GMLocal7/.g0ldm1n3", ios::out );
  21. }
  22.  
  23. Sleep( 10000 ); // Sleep for 10 secs
  24. }
  25. return 0;
  26. }

thanks in advance for your help!
Last edited by mario20055; Jul 2nd, 2009 at 4:32 pm. Reason: I need to be more specific when mentioning on the deletion.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: C++ -- Creating file but unable to delete it

 
0
  #2
Jul 2nd, 2009
Open the file with the sharing flag set will give you some flexibility.


If you want your program to delete a file clean due to corruption, etc. you can use one of the following!


Simple CRun time library call.

  1. #include <io.h>
  2.  
  3. remove( "HowdyDoody.txt" );

And since you're using windows
#include <windows.h>

  1. DeleteFile( "HowdyDoody.txt" );
Last edited by wildgoose; Jul 2nd, 2009 at 4:25 pm. Reason: answer adjustment
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: C++ -- Creating file but unable to delete it

 
0
  #3
Jul 2nd, 2009
Create a function, does file exist. If it fails then create it!

  1. bool fileExist( const char * const pFName )
  2. {
  3. return (bool)(0 == _access( pFName, 0));
  4. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ -- Creating file but unable to delete it

 
0
  #4
Jul 3rd, 2009
Its not necessary to test for file existance beforehand. Just open like this: ios::in | ios::out | ios::ate . This will append text to the end of the file every time the program is run.
  1. int main(int argc, char* argv[])
  2. {
  3. fstream out("HelloWorld.txt", ios::in | ios::out | ios::ate );
  4. if( out.is_open() )
  5. {
  6. out << "Hello World\n";
  7. }
  8. else
  9. cout << "Failed\n";
  10. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,878
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: C++ -- Creating file but unable to delete it

 
0
  #5
Jul 3rd, 2009
Originally Posted by mario20055 View Post
when I try to delete the file Windows is telling me that the file is begin used by another program.

  1. Note: I don't want my EXE to delete the file..... The USER will be deleting this file.
  2.  
As long as your program is busy with the file, Windows won't delete it. So I'd take Ancient Dragon's code, but put a close() in there so that the user can access the file.

int main(int argc, char* argv[])
{
    fstream out("HelloWorld.txt", ios::in | ios::out | ios::ate );
    if( out.is_open() )
    {
        out << "Hello World\n";
    }
    else
        cout << "Failed\n";

    // We're done:
    out.close();
}
Last edited by niek_e; Jul 3rd, 2009 at 10:41 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ -- Creating file but unable to delete it

 
0
  #6
Jul 3rd, 2009
fstream auto closes the file when the function is finished, and that's why I didn't do it in main(). But you have to call close() if you wanted to delete the file in the same function that opened it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 3
Reputation: mario20055 is an unknown quantity at this point 
Solved Threads: 0
mario20055 mario20055 is offline Offline
Newbie Poster

Re: C++ -- Creating file but unable to delete it

 
0
  #7
Jul 3rd, 2009
I would like to thank you all that reply for your great help, and support... both suggestions worked fine and also adding a simple
file.close(); before the sleep worked as well.

thank you guys... you rock!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC