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.

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

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:

#include "stdafx.h"
#include "windows.h" 
#include "iostream"
#include "fstream"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{ 
    HWND hWnd = GetConsoleWindow(); 
    ShowWindow( hWnd, SW_HIDE ); // hide the console window
 
while( true )
{
    fstream file;
    file.open( "C:/Program Files/GMLocal7/.g0ldm1n3", ios::in );
 
    if( file.is_open()  == false )
    {
        file.open( "C:/Program Files/GMLocal7/.g0ldm1n3", ios::out );
    }
 
     Sleep( 10000 ); // Sleep for 10 secs
}
    return 0; 
}

thanks in advance for your help!

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.

#include <io.h>

remove( "HowdyDoody.txt"  );

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

DeleteFile( "HowdyDoody.txt" );

Create a function, does file exist. If it fails then create it!

bool fileExist( const char * const pFName )
{
      return (bool)(0 == _access( pFName, 0));
}

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.

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";
}

when I try to delete the file Windows is telling me that the file is begin used by another program.

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

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();
}

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.

commented: Did not know that :) +20

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.