1. ## Remove function

I suppose this post could be taken wrongly -- That I would create a type of virus or harmful application but rest assured I'm not trying to accomplish that.

--

Okay, I wanted to learn how to delete a file in C. I dug this up from your web site... I don't understand it. So far I'm pretty basic with C. I'm using Visual Studio 6.0... Okay, so I want to delete a file, say text.txt.. How do I do that? So far I have

Code:
#include <stdio.h>

void main()
{
int remove(text.txt);
}
I know it's wrong, so maybe someone can get a drift on what I'm trying to do.

Alex

EDIT===

What does the const char thing mean?

2. Well you almost had it. The text string should be in quotes and the return type shouldn't be there.
Code:
remove("text.txt");
For this to work text.txt must be in your program's current working directory.

You might want to check weather deletion was successfull
Code:
if (remove("text.txt"))//remove returned non-zero
printf("Error deleting file\n");

3. Try
Code:
#include <stdio.h>

int main()
{
remove("text.txt");
}
This attempts to remove a file named text.txt in the current working directory where your program is executed. It doesn't check if that succeeded. It may be better to supply path information (eg remove("C:/test.txt") or remove(C:\\test.txt") [note double backslashes here in code] to remove a file at the root of the C: drive under windows.

The remove() function tends to fail if a file doesn't exist (for obvious reasons). It also tends to fail if the operating system prevents the file being deleted (eg file protections, another process has the file open, etc) and does not tend to work for deleting directories. To get around those limitations, you normally have to fall on system-specific functions (eg change file protections, recursively delete files in a directory and then a specific function call to remove the directory, etc etc).

4. Ok... I'll try it in a second... Is there a function that ends a process? I'm going to be creating some malware removal tools and I'm curious if there is a function that ends a program process.

Edit===

I compiled the script, using grumpys example and I placed the exe and txt file in the same file and it didn't delete it.

5. That code is corect. Did you double click the exe? because if you started it from your IDE or typed it's name into a command prompt then the working directory could be different. Use a full path if you want to be sure.

6. I used command prompt to open it... I'll try double-clicking it.

7. Originally Posted by alex_dude_122
Ok... I'll try it in a second... Is there a function that ends a process?
Not in standard C. The means of doing this depend on your operating system.
Originally Posted by alex_dude_122
I compiled the script, using grumpys example and I placed the exe and txt file in the same file and it didn't delete it.
First a couple of nits: you compile source code. Scripts are files that are interpreted by another program when you actually run them (for example, when you execute a perl script, the perl program interprets the perl script on the fly).

There are all sorts of possible reasons the file wasn't deleted. For example, if you are running from the windows NT command prompt, the command "C:\yourpath\yourprogram" will only delete c:\yourpath\text.txt if your current working directory is c:\yourpath. If your current directory is c:\another, then the program will attempt to delete c:\another\text.txt. If there are spaces in the path (or program name) then you need to specify the quotes on the command line. If you're running the program in the debugger (in the IDE) the working directory depends on settings of your IDE.

8. Not in standard C. The means of doing this depend on your operating system.
Is this availible in C++? I have Windows XP Home.

First a couple of nits: you compile source code. Scripts are files that are interpreted by another program when you actually run them (for example, when you execute a perl script, the perl program interprets the perl script on the fly).
Ok.. I double clicked on the exe, it removed the file... Thank you.

9. Originally Posted by alex_dude_122
Is this availible in C++? I have Windows XP Home.
No the function to kill another process is part of the windows API and it's name is TerminateProcess().

10. Originally Posted by Quantum1024
No the function to kill another process is part of the windows API and it's name is TerminateProcess().
#include <windows.h>