| | |
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:
Solved Threads: 0
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.
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:
thanks in advance for your help!
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.
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
#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!
Last edited by mario20055; Jul 2nd, 2009 at 4:32 pm. Reason: I need to be more specific when mentioning on the deletion.
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.
And since you're using windows
#include <windows.h>
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.
C Syntax (Toggle Plain Text)
#include <io.h> remove( "HowdyDoody.txt" );
And since you're using windows
#include <windows.h>
C Syntax (Toggle Plain Text)
DeleteFile( "HowdyDoody.txt" );
Last edited by wildgoose; Jul 2nd, 2009 at 4:25 pm. Reason: answer adjustment
Create a function, does file exist. If it fails then create it!
C Syntax (Toggle Plain Text)
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. C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
Note: I don't want my EXE to delete the file..... The USER will be deleting this 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.
![]() |
Similar Threads
- Creating a new file type - and creating Search function (C#)
- creating a file in C ? (C)
- Reading, Writing and Creating a File (C)
- Unable to Delete Folders on my D: Drive Only?? (Windows NT / 2000 / XP)
- CANNOt Delete file No option to delete,rename,move file (Windows NT / 2000 / XP)
- Unable to delete SQL records (MS SQL)
- cannot repair from hacktool rootkit,unable to delete msdirectx.sys (Viruses, Spyware and other Nasties)
- Unable to get rid of Hacktool.rootkit virus(/Trojan) (Viruses, Spyware and other Nasties)
- file wont delete (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: slowly but surely
- Next Thread: Best C++ Compiler
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






