Need Help: C++ Delete Known File

D

da Vinci

Greetings,

Onwards with the school studying.

Working on a program and need to delete a file from a known location
on the hard drive but cannot get anything I do to work.

I have tried to use the remove function that is included with <cstdio>
but cannot get it to work properly. My reference book has the
following....

int remove(const char *fname);

and I have found information on the internet using the same above
along with an if statement. When it returns -1 it prints out "Not
deleted" and when it returns 0 if prints "deleted".

However, it never deletes the file.

I have tried to use the system("del c:\deleteme.txt"); command as
well. When I have tweaked the above command, it continues to say the
file doesnt exist even though the path it shows is EXACTLY where the
file is.

I hate asking for code, in fact I think there is something in the FAQ
saying not to ask for it.... but can someone set me straight here.
Either I cannot figure out how to use the remove() properly or
something is afu. :)

Thanks gents.
 
L

Leor Zolman

Greetings,

Onwards with the school studying.

Working on a program and need to delete a file from a known location
on the hard drive but cannot get anything I do to work.

I have tried to use the remove function that is included with <cstdio>
but cannot get it to work properly. My reference book has the
following....

int remove(const char *fname);

and I have found information on the internet using the same above
along with an if statement. When it returns -1 it prints out "Not
deleted" and when it returns 0 if prints "deleted".

However, it never deletes the file.

I have tried to use the system("del c:\deleteme.txt"); command as

Try c:\\deleteme.txt, or better yet, the form that would have saved /me/
lots of debugging time if I'd realized it was equivalent, c:/deleteme.txt.
[Note: Usually, I don't notice this kind of bug until I'm single-stepping
my program right up to the system/fopen/ifstream/whatever call, and looking
at the debugger dump of the command string...)
-leor
 
D

David Harmon

On Sun, 09 May 2004 01:53:20 GMT in comp.lang.c++, da Vinci
I have tried to use the system("del c:\deleteme.txt"); command as

This issue is covered in Marshall Cline's C++ FAQ. See the topic
'[15.16] Why can't I open a file in a different directory such as
"..\test.dat"?" It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

The usual answer to that is the same in C++ as it is in C, and is
covered in Q. 19.17 of Steve Summit's C FAQ. It is always good to
check the FAQ before posting. You can get the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html
 
D

da Vinci

This issue is covered in Marshall Cline's C++ FAQ. See the topic
'[15.16] Why can't I open a file in a different directory such as
"..\test.dat"?" It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

The usual answer to that is the same in C++ as it is in C, and is
covered in Q. 19.17 of Steve Summit's C FAQ. It is always good to
check the FAQ before posting. You can get the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html

I have read them.

The issue is not in using a \ or a /. I cannot get it to delete the
file, regardless of what I use.

Example:

#include <cstdio>

int main()
{
if (remove("SEEBELOW") == -1)
perror ("Error deleting file");
else
puts ("File deleted");

return 0;
}

SEEBELOW is the file name and path that I have tried many different
things with. c:/deleteme.txt ../deleteme.txt ect

I have even created that txt file in every directory from c:\ to the
directory where the .exe is at (debug in projects folder).

This code is also right off the net and still doesnt work. I am
clueless on it.
 
M

Mike Wahler

da Vinci said:
This issue is covered in Marshall Cline's C++ FAQ. See the topic
'[15.16] Why can't I open a file in a different directory such as
"..\test.dat"?" It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

The usual answer to that is the same in C++ as it is in C, and is
covered in Q. 19.17 of Steve Summit's C FAQ. It is always good to
check the FAQ before posting. You can get the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html

I have read them.

The issue is not in using a \ or a /. I cannot get it to delete the
file, regardless of what I use.

Example:

#include <cstdio>

int main()
{
if (remove("SEEBELOW") == -1)
perror ("Error deleting file");
else
puts ("File deleted");

return 0;
}

SEEBELOW is the file name and path that I have tried many different
things with. c:/deleteme.txt ../deleteme.txt ect

I have even created that txt file in every directory from c:\ to the
directory where the .exe is at (debug in projects folder).

This code is also right off the net and still doesnt work. I am
clueless on it.

You're not listening.

remove("c:\\deleteme.txt");

You must also ensure that your operating system is not
preventing the deletion (via e.g. 'permission' settings).


-Mike
 
J

John Harrison

da Vinci said:
This issue is covered in Marshall Cline's C++ FAQ. See the topic
'[15.16] Why can't I open a file in a different directory such as
"..\test.dat"?" It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

The usual answer to that is the same in C++ as it is in C, and is
covered in Q. 19.17 of Steve Summit's C FAQ. It is always good to
check the FAQ before posting. You can get the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html

I have read them.

The issue is not in using a \ or a /. I cannot get it to delete the
file, regardless of what I use.

Here's the problem. Either you are trying to delete a file you cannot delete
because you don't have the necessary permissions. Or you are making some
sort of silly mistake. It's impossible for anyone but yourself to tell if
the former is the case. And it is impossible for anyone here to tell if the
latter is the case unless you POST THE EXACT CODE THAT DOESN'T WORK. It's
not good enough to post your description of what you think might be going
wrong, or an imprecise description of all the different things you have
tried, as you say you are clueless. Post the exact code, don't type it in,
cut and paste it from your compiler. Then we should get it sorted out pretty
quickly.

Also might help to mention what operating system and compiler you are using.

john
 
D

da Vinci

Here's the problem. Either you are trying to delete a file you cannot delete
because you don't have the necessary permissions. Or you are making some
sort of silly mistake. It's impossible for anyone but yourself to tell if
the former is the case. And it is impossible for anyone here to tell if the
latter is the case unless you POST THE EXACT CODE THAT DOESN'T WORK. It's
not good enough to post your description of what you think might be going
wrong, or an imprecise description of all the different things you have
tried, as you say you are clueless. Post the exact code, don't type it in,
cut and paste it from your compiler. Then we should get it sorted out pretty
quickly.

Also might help to mention what operating system and compiler you are using.

john

Sorry, got caught up in the problem and forgot to give the details.

I am running Windows XP Pro SP1 and using Microsoft Visual C++ 6.0
with Visual Studio 6 SP5 update.

Here is an exact paste of the code I am working on. I left everything
I tried in the program and just // off each one as I went on to a new
one. This way you can see what I have tried to do in the past and
where I am at currently. I have also checked the permission settings
on all of the files I placed through out the directories and none of
them are hidden or read only. Unless Windows XP sets another type of
permission that I do not know about (not listed on a right click -
properties) then that is good.

Here is the code:

#include <cstdio>

int main()
{
/*if( remove( "../deleteme.txt" ) == -1 )
perror( "Error deleting file" );
else
puts( "File successfully deleted" );*/

//remove("deleteme.txt");
//remove("c:/deleteme.txt");
//remove("../deleteme.txt");
//remove("c:\\deleteme.txt");
//remove("c:\deleteme.txt");

remove("/deleteme.txt");

return 0;

}
 
J

Jorge Rivera

However, it never deletes the file.

If it remove returns -1, remember to use errno
(I will assume using std somewhere...)
Eg.

#include <cerrno>
....
if(-1 == remove(filename))
{
cout<<"Could not delete: "<<filename<<" due to: "
<<strerror(errno)<<endl;
}
....


This shoudl tell you why, and the output of the filename might also tell
you something about the way you construct your filename array...

Let mw know how it goes

Jorge L.
 
J

John Harrison

da Vinci said:
Sorry, got caught up in the problem and forgot to give the details.

I am running Windows XP Pro SP1 and using Microsoft Visual C++ 6.0
with Visual Studio 6 SP5 update.

Here is an exact paste of the code I am working on. I left everything
I tried in the program and just // off each one as I went on to a new
one. This way you can see what I have tried to do in the past and
where I am at currently. I have also checked the permission settings
on all of the files I placed through out the directories and none of
them are hidden or read only. Unless Windows XP sets another type of
permission that I do not know about (not listed on a right click -
properties) then that is good.

Oh yes, Windows XP has lots of other types of permissions. Right click -
permissions - security tab.
Here is the code:

#include <cstdio>

int main()
{
/*if( remove( "../deleteme.txt" ) == -1 )
perror( "Error deleting file" );
else
puts( "File successfully deleted" );*/

//remove("deleteme.txt");
//remove("c:/deleteme.txt");
//remove("../deleteme.txt");
//remove("c:\\deleteme.txt");
//remove("c:\deleteme.txt");

remove("/deleteme.txt");

return 0;

}

Well I tried all your variations and they all work for except
"c:\deleteme.txt" which is wrong (should be \\ not \). They don't all delete
files in the same directory mind you, but when I put a file in the right
place it deletes it.

So we have a bit of a mystery. The one thing you haven't said yet which
might help is what error message you get from the perror call.

Here's some possiblities, but none seem very likely

1) You don't have the permissions to delete the file. Since you seem to be
creating the file this seems unlikely. But try deleteing the file from the
desktop, if you can do that, then a program you run should also be able to
delete the file.

2) Some other program has the file open. For instance maybe you are using an
editor to create the file but you haven't closed down that editor before you
try to delete the file.

3) In all your different attempts with files in different places you haven't
managed to get the combination of file location and program code correct
yet.

Another issue, can you delete a file when its in the same directory as your
program code? I.e. when you just try

remove("deleteme.txt");

and deleteme.txt is in the same place as the code above.

Another thing to try, use this code

#include <windows.h>

int main()
{
DWORD err_code = 0;
if (!DeleteFile("c:\\deleteme.txt"))
err_code = GetLastError();
}

I'd be interested to know whether this program deletes the file, and if not
what the value of err_code is.

john
 
J

John Harrison

1) You don't have the permissions to delete the file. Since you seem to be
creating the file this seems unlikely. But try deleteing the file from the
desktop, if you can do that, then a program you run should also be able to
delete the file.

2) Some other program has the file open. For instance maybe you are using an
editor to create the file but you haven't closed down that editor before you
try to delete the file.

3) In all your different attempts with files in different places you haven't
managed to get the combination of file location and program code correct
yet.

Another possibility

4) You think you are compiling the code you posted but in fact you've made
some mistake in your project settings somewhere and in fact you're compiling
and running a completely different program.

When things seem insane you need to run a few sanity checks.

john
 
L

Leor Zolman

I am running Windows XP Pro SP1 and using Microsoft Visual C++ 6.0
with Visual Studio 6 SP5 update.

Here is an exact paste of the code I am working on. I left everything
I tried in the program and just // off each one as I went on to a new
one. This way you can see what I have tried to do in the past and
where I am at currently. I have also checked the permission settings
on all of the files I placed through out the directories and none of
them are hidden or read only. Unless Windows XP sets another type of
permission that I do not know about (not listed on a right click -
properties) then that is good.

Here is the code:

#include <cstdio>

int main()
{
/*if( remove( "../deleteme.txt" ) == -1 )
perror( "Error deleting file" );
else
puts( "File successfully deleted" );*/

//remove("deleteme.txt");
//remove("c:/deleteme.txt");
//remove("../deleteme.txt");
//remove("c:\\deleteme.txt");
//remove("c:\deleteme.txt");

remove("/deleteme.txt");

return 0;

}

You might try just "fopen"-ing the file first and see if that succeeds.
That will tell you if the basic pathname is being processed as you expect.
Of course, close it before attempting to delete it.

Also, have you tested by creating a new file and then immediately trying to
delete it? That would eliminate possibilities such as the file being held
open, having weird permissions, etc. I'd try that both by creating the file
outside of the program, and also by creating it /within/ the program (e.g.,
using fopen/fputs/fclose to create it first), just to see if the two behave
any differently.

Good luck,
-leor
 
D

DaVinci

#include <windows.h>

int main()
{
DWORD err_code = 0;
if (!DeleteFile("c:\\deleteme.txt"))
err_code = GetLastError();
}

I ran this code and the only thing it says is "Press any key to
continue". There are no error codes or anything else displayed.

The file is still there though.

In reply to another post: I have no security tab. All I have is Right
Click > Properties > General / Summary tabs. I am logged in as an
administrator with full access on the computer. I have ensured that
the read only and hidden blocks are unchecked and went into the
'advanced' area and made sure everything was good there.

It works for you and not me.... that is frustrating!!
 
J

John Harrison

DaVinci said:
I ran this code and the only thing it says is "Press any key to
continue". There are no error codes or anything else displayed.

Well no, you have to add a print statement

#include <windows.h>
#include <cstdio> // add this

int main()
{
DWORD err_code = 0;
if (!DeleteFile("c:\\deleteme.txt"))
err_code = GetLastError();
printf("error code = %lu\n", err_code); // add this
}

Sorry I didn't think I would have to spell that out.

What about the error message you get from your original code?

if( remove( "../deleteme.txt" ) == -1 )
perror( "Error deleting file" );
else
puts( "File successfully deleted" );

That should tell you something useful.

john
 
J

Jean Charbonneau

the remove function calls system functions, such as "del". Does it work in a
dos box, when you try to delete a file ? Maybe your PATH environnement
variable is not set to the windows system ?
 
J

John Harrison

Jean Charbonneau said:
the remove function calls system functions, such as "del". Does it work in a
dos box, when you try to delete a file ? Maybe your PATH environnement
variable is not set to the windows system ?


On my system (which is MSVC++ 7.1 so not the same as the OP's) remove calls
the Windows API function DeleteFile, which is why I advised the OP do to the
same.

I'd be very surprised if the remove API call required a correct PATH
environment variable to work, but maybe the OP could give it a try.

john
 
J

Jean Charbonneau

John Harrison said:
in


On my system (which is MSVC++ 7.1 so not the same as the OP's) remove calls
the Windows API function DeleteFile, which is why I advised the OP do to the
same.

I'd be very surprised if the remove API call required a correct PATH
environment variable to work, but maybe the OP could give it a try.

john

I was wondering after reading the remove doc which is there ->
http://www.cplusplus.com/ref/cstdio/remove.html

It says "It is compiled as a call to the system function for deleting files
(unlink, erase or del)."

I understand then, that it calls the del on windows environnement, and
therefore I was wondering if then depending on the PATH value, that call
would work or not, tell me if I'm wrong, I'm a beginner somehow.
 
L

Leor Zolman

I was wondering after reading the remove doc which is there ->
http://www.cplusplus.com/ref/cstdio/remove.html

It says "It is compiled as a call to the system function for deleting files
(unlink, erase or del)."

I understand then, that it calls the del on windows environnement, and
therefore I was wondering if then depending on the PATH value, that call
would work or not, tell me if I'm wrong, I'm a beginner somehow.

I think you're confusing "system calls" (either when they're invoked
directly from a C/C++ program, or as a result of running command
interpreters' "built-in" commands such as cd on Win/Unix, del on Windows,
etc.) with the running of "external" or "transient" commands. Since, under
Windows, even the command line's "del" is built in to the command
processor, it wouldn't rely on the setting of PATH (which, under Windows,
is used primarily to locate executable commands in the case when they're
not present in the current working directory). In any case, library
functions such as "remove" would never in their right mind invoke a
command processor, since removing is a system API facility that can be
invoked directly via a direct API call...again, without any use for the
PATH setting. In fact, I doubt any library functions ever have any need to
invoke the command processor, with the exception of those designed
specifically for the purpose of invoking a command processor with various
configurations of arguments. Those, such as system(), would be the only
ones that the PATH setting might affect.
-leor
 
D

DaVinci

#include <windows.h>
#include <cstdio> // add this

int main()
{
DWORD err_code = 0;
if (!DeleteFile("c:\\deleteme.txt"))
err_code = GetLastError();
printf("error code = %lu\n", err_code); // add this
}

On this code, I get an error code of 2.

I was thinking the same on the print statement needing to be added but
at this point, I didnt want to mess with anything. Seeing as how this
'should' be working and it isnt, I wanted to do exactly as you said to
see if we could get it figured out.

As for the original code you asked about.....

if( remove( "c:/deleteme.txt" ) == -1 )
perror( "Error deleting file" );
else
puts( "File successfully deleted" );

I always get a "No such file or directory" error. However, as I said
before, I have that file in every directory from C:\ all the way to
the debug folder where the .exe is being compiled and executed.
 
J

John Harrison

DaVinci said:
On this code, I get an error code of 2.

I was thinking the same on the print statement needing to be added but
at this point, I didnt want to mess with anything. Seeing as how this
'should' be working and it isnt, I wanted to do exactly as you said to
see if we could get it figured out.

As for the original code you asked about.....

if( remove( "c:/deleteme.txt" ) == -1 )
perror( "Error deleting file" );
else
puts( "File successfully deleted" );

I always get a "No such file or directory" error. However, as I said
before, I have that file in every directory from C:\ all the way to
the debug folder where the .exe is being compiled and executed.

Well, what can I say, error code 2 means "The system cannot find the file
specified", perror says basically the same.

I'm stumped I'm afraid. However this is very clearly a Windows issue not a
C++ issue, there is nothing wrong with your code from a C++ point of view.
Perhaps you could ask again on a Windows programming group like
Someone with more Windows
expertise might be able to answer.

If I think of anything I'll post again.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top