Remove function

This is a discussion on Remove function within the C Programming forums, part of the General Programming Boards category; I suppose this post could be taken wrongly -- That I would create a type of virus or harmful application ...

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    Grandrapids Michigan
    Posts
    7

    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?
    Last edited by alex_dude_122; 05-21-2006 at 09:41 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    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. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    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. #4
    Registered User
    Join Date
    May 2006
    Location
    Grandrapids Michigan
    Posts
    7
    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.
    Last edited by alex_dude_122; 05-21-2006 at 09:57 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    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. #6
    Registered User
    Join Date
    May 2006
    Location
    Grandrapids Michigan
    Posts
    7
    I used command prompt to open it... I'll try double-clicking it.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    Quote 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.
    Quote 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. #8
    Registered User
    Join Date
    May 2006
    Location
    Grandrapids Michigan
    Posts
    7
    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.
    Last edited by alex_dude_122; 05-22-2006 at 01:00 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote 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. #10
    Registered User
    Join Date
    May 2006
    Location
    Grandrapids Michigan
    Posts
    7
    Quote Originally Posted by Quantum1024
    No the function to kill another process is part of the windows API and it's name is TerminateProcess().
    Windows API? Please explain.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    The windows API (applications programming interface) is a set of functions provided by windows that your program can make use of. These functions are useable from c, c++ and pretty much any other language but they are not part of these languages themselves. include windows.h to use this function
    Code:
    #include <windows.h>
    Using operating system specific functions means that your c/c++ program will only compile on a windows platform where as a standard c/c++ program will compile and run on any supported platform.

  12. #12
    Registered User
    Join Date
    May 2006
    Location
    Grandrapids Michigan
    Posts
    7
    Thank you -- I purchased some C For Dummies books that where written in '96 and I like them, easy to understand but I'm already thinking of some more advanced stuff. Does anyone know of any online tutorials or books that teach more advanced C stuff like connecting to the internet, etc.

    Thanks again

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Again c doesn't contain any networking support which is why it isn't covered in c books. For that you need to rely on your operating system. Windows provides winsock for this you can find some information in the networking forum particularly in the stickied post at the top..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21