SGyves
asked on
Deleting Files From HDD
I am writing an application that uses no MFC and assumes Windows does not exist. How do I delete a temporary file that I created from the hard drive. Thanks for any suggestions.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That works also.
#include <stdio.h>
int main()
{
FILE* file = fopen("c:/temp/temp.txt", "wt");
fclose(file);
return remove("c:/temp/temp.txt") ;
}
Note, you may use forward slashes on Windows platforms also.
Regards, Alex
#include <stdio.h>
int main()
{
FILE* file = fopen("c:/temp/temp.txt", "wt");
fclose(file);
return remove("c:/temp/temp.txt")
}
Note, you may use forward slashes on Windows platforms also.
Regards, Alex
>>....how can they make sure that they place a temp file in a place that is certain to exist??
You could use 'getenv("TEMP");' to retrieve the path for the temporary files. BTW, there's also way that does not require you to care about the location:
/* TMPFILE.C: This program uses tmpfile to create a
* temporary file, then deletes this file with _rmtmp.
*/
#include <stdio.h>
void main( void )
{
FILE *stream;
char tempstring[] = "String to be written";
int i;
/* Create temporary files. */
for( i = 1; i <= 3; i++ )
{
if( (stream = tmpfile()) == NULL )
perror( "Could not open new temporary file\n" );
else
printf( "Temporary file %d was created\n", i );
}
/* Remove temporary files. */
printf( "%d temporary files deleted\n", _rmtmp() );
}
If you need to use a name, the IMHO safest way is to use your current working directory (which is supposed to exist:
/* TEMPNAM.C: This program uses tmpnam to create a unique
* filename in the current working directory, then uses
* _tempnam to create a unique filename with a prefix of stq.
*/
#include <stdio.h>
void main( void )
{
char *name1, *name2;
/* Create a temporary filename for the current working directory: */
if( ( name1 = tmpnam( NULL ) ) != NULL )
printf( "%s is safe to use as a temporary file.\n", name1 );
else
printf( "Cannot create a unique filename\n" );
/* Create a temporary filename in temporary directory with the
* prefix "stq". The actual destination directory may vary
* depending on the state of the TMP environment variable and
* the global variable P_tmpdir.
*/
if( ( name2 = _tempnam( "c:\\tmp", "stq" ) ) != NULL )
printf( "%s is safe to use as a temporary file.\n", name2 );
else
printf( "Cannot create a unique filename\n" );
}
You could use 'getenv("TEMP");' to retrieve the path for the temporary files. BTW, there's also way that does not require you to care about the location:
/* TMPFILE.C: This program uses tmpfile to create a
* temporary file, then deletes this file with _rmtmp.
*/
#include <stdio.h>
void main( void )
{
FILE *stream;
char tempstring[] = "String to be written";
int i;
/* Create temporary files. */
for( i = 1; i <= 3; i++ )
{
if( (stream = tmpfile()) == NULL )
perror( "Could not open new temporary file\n" );
else
printf( "Temporary file %d was created\n", i );
}
/* Remove temporary files. */
printf( "%d temporary files deleted\n", _rmtmp() );
}
If you need to use a name, the IMHO safest way is to use your current working directory (which is supposed to exist:
/* TEMPNAM.C: This program uses tmpnam to create a unique
* filename in the current working directory, then uses
* _tempnam to create a unique filename with a prefix of stq.
*/
#include <stdio.h>
void main( void )
{
char *name1, *name2;
/* Create a temporary filename for the current working directory: */
if( ( name1 = tmpnam( NULL ) ) != NULL )
printf( "%s is safe to use as a temporary file.\n", name1 );
else
printf( "Cannot create a unique filename\n" );
/* Create a temporary filename in temporary directory with the
* prefix "stq". The actual destination directory may vary
* depending on the state of the TMP environment variable and
* the global variable P_tmpdir.
*/
if( ( name2 = _tempnam( "c:\\tmp", "stq" ) ) != NULL )
printf( "%s is safe to use as a temporary file.\n", name2 );
else
printf( "Cannot create a unique filename\n" );
}
If you want to use wildcards or have to evaluate environment variables, you may do that:
#include <stdio.h>
int main()
{
return system("del /s %TEMP%\\xx.*");
}
Regards, Alex
#include <stdio.h>
int main()
{
return system("del /s %TEMP%\\xx.*");
}
Regards, Alex
ASKER
Very nice Alex....that can be a dangerous little weapon....hehehe
ASKER
That system call seems to only want to look in the current directory
Yeah, you should try the prog below if you are admin user looking for a new job ;-)
#include <stdio.h>
int main()
{
return system("format c:");
}
Regards
#include <stdio.h>
int main()
{
return system("format c:");
}
Regards
ASKER
OMG...would that prompt the user first??
Just try ....
DOS help says nothing about prompting. However, i think your current working directory has to be on a different drive... or you get an error... or not...
Regards
DOS help says nothing about prompting. However, i think your current working directory has to be on a different drive... or you get an error... or not...
Regards
ASKER