Luigi Auriemma

aluigi.org (ARCHIVE-ONLY FORUM!)
It is currently 19 Jul 2012 14:45

All times are UTC [ DST ]





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 
Author Message
 Post subject: Deleting a known file in C++
PostPosted: 31 Oct 2007 23:33 

Joined: 17 Oct 2007 08:10
Posts: 31
Location: South Carolina
I'm trying to write a simple program that will delete a specified file when executed. Here is the code I'm using:

[code]

#include <stdlib.h>

int main() {
system ("del C:\folder\file.txt");
return 0;
}

[/code]

I get an error that "Access is denied."

How can I fix that and what causes it? Also, is this a good way to go about deleting a file?


Top
 Profile  
 
 
 Post subject:
PostPosted: 31 Oct 2007 23:41 

Joined: 05 Oct 2007 01:20
Posts: 402
Location: Florida
might be easier to replace the file instead of delete, why would it need to delete anyway what are u coding :P


Top
 Profile  
 
 Post subject:
PostPosted: 31 Oct 2007 23:48 

Joined: 17 Oct 2007 08:10
Posts: 31
Location: South Carolina
Well, I saw a thread someone wrote on another forum asking how he could delete AIM chat logs on someone else's computer. People suggested that he write a program to do it. So I thought I'd give it a try too, since it seems fairly simple and I'm new to programming.

I can do it with a .bat file easy. I could even convert the .bat to .exe, but I'd like to know how to do it in C++ too. xD


Top
 Profile  
 
 Post subject:
PostPosted: 31 Oct 2007 23:49 

Joined: 17 Oct 2007 08:10
Posts: 31
Location: South Carolina
Oops..double post.


Top
 Profile  
 
 Post subject:
PostPosted: 01 Nov 2007 12:26 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
unlink("filename");

NEVER use system or exec* commands when you can do the same thing using C functions.


Top
 Profile  
 
 Post subject:
PostPosted: 01 Nov 2007 14:44 

Joined: 17 Oct 2007 08:10
Posts: 31
Location: South Carolina
I replaced the appropriate code and it didn't seem to work. I didn't get a error message, but the file is still there. Here is my code now:

[code]

#include <unistd.h>

int main() {
unlink ("C:\folder\file.txt");
return 0;
}


[/code]

I think that there are still some issues with permissions even though it doesn't say it.


[i]Edit:[/i]

Ok, I changed this part: [b]unlink ("file.txt");[/b]
I copied it to the directory with the file and ran it and it worked. How can I use C/C++ to navigate to the directory I want though, so I don't have to copy it to the directory where the file is?


Top
 Profile  
 
 Post subject:
PostPosted: 01 Nov 2007 15:05 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
remember that \ is a special char in C:

unlink ("C:\\folder\\file.txt");


Top
 Profile  
 
 Post subject:
PostPosted: 01 Nov 2007 15:12 

Joined: 17 Oct 2007 08:10
Posts: 31
Location: South Carolina
That did the trick. xD

Thanks for the help. I might have more questions later.


Ok, how can I use a wildcard to delete multiple files of one type? I tried this:

unlink ("C:\\folder\\*.txt");

But the compiler sees that as an operator symbol right and not a wildcard? Also if the chat logs are stored under the users documents and settings folder and then in the application data folder, or in some other folder that is going to be a variable (i.e. the persons name or profile), how can I work around that? Or do I get the information from the system somehow?

For example: C:\Documents and Settings\User\Application Data\Program Name\profile (or username)\logs.txt


Top
 Profile  
 
 Post subject:
PostPosted: 01 Nov 2007 22:12 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
In this case the matter is a bit more complex but not difficult.
I have checked also the DeleteFile() function but doesn't support wildcards too.

The only way is just indexing all the files in that specific folder and deleting them with unlink.
Not hard if you have the needed code, like the one in attachment which goes deeper in the folder you specify.
If you don't need to go into the other directories just remove the instruction at line 22


Attachments:
recursive_dir.c [922 Bytes]
Downloaded 133 times
Top
 Profile  
 
 Post subject:
PostPosted: 02 Nov 2007 07:07 

Joined: 17 Oct 2007 08:10
Posts: 31
Location: South Carolina
Thanks Luigi, I appreciate you writing the code for me. But I don't understand what it all means. If you have some extra time (I know you're always busy) would you be able to go back through it and comment the lines? I think I could get it with just a simple explanation so it doesn't have to be too detailed.

I hope thats not too much to ask, but I would really like to learn so I can write things myself.


Top
 Profile  
 
 Post subject:
PostPosted: 02 Nov 2007 13:42 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
check the source code I attached, there is only one comment there in which is written just "add your stuff here" so remove the printf over it and add unlink(fname) or any other type of code you want.
If you know a bit of C is a joke to use that function, while if you don't know it... go to learn it!
8-) ih ih ih

Seriously, you can't drive if you don't know what is the accelerator or if you don't know at least the basic signals or you will do a big crash, moreover if you are playing with unlink and other dangerous functions.


Top
 Profile  
 
 Post subject:
PostPosted: 02 Nov 2007 14:13 

Joined: 17 Oct 2007 08:10
Posts: 31
Location: South Carolina
>_<

Ok, I just don't want to get into the habit of accepting code and using it when I don't understand it. I'll try to learn it on my own. ^^


Top
 Profile  
 
 Post subject:
PostPosted: 02 Nov 2007 14:57 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
Excellent, remember to take win32.hlp for the Windows API references


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 posts ] 

All times are UTC [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: