Deleting files.

This is a discussion on Deleting files. within the C Programming forums, part of the General Programming Boards category; I'm trying to make a program that deletes a file called biblio.exe which is in the root dir. I try ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    75

    Question Deleting files.

    I'm trying to make a program that deletes a file called biblio.exe which is in the root dir. I try to accomplish that by first going to the root dir (I think I'm correct) and then opening the file and then using remove.
    But it doesnt seem to work, what's wrong?? It says "error en remove".

    #include "stdio.h"
    #include "stdlib.h"

    main(void)
    {
    FILE *fp;

    system("cd..");
    system("cd..");
    system("cd..");
    system("cd..");

    if((fp=fopen("biblio.exe","rb"))==NULL){
    printf("Error al abrir archivo!");
    exit(1);
    }

    if(remove(fp)) printf("Error en remove!");
    }
    ---Programming is like roaming, you never know where you'll end at------

    If 'here' is pronounced as 'hear', why 'there' isnt pronounced as 'dear'??

    paninaro3000@yahoo.com

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are using remove incorrectly.

    remove( "myfilename" );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    oh and try system("cd \"); to get to the root dir

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by RpiMatty
    oh and try system("cd \"); to get to the root dir
    Why bother?

    remove( "c:/myprogram.exe" );

    Although to be fair, you could be on another drive than C.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    What OS are you trying to do this in? If it is under Windows, i doubt this will ever work, because you have opened this file, and Windows will not allow you to delete any file when it is open or being accessed.
    On the other hand, if it is UNIX, you might try:
    Code:
    ret = remove("biblio.exe");
    ret = unlink("biblio.exe");
    If ret is not equal to 0, then 'perror()', you'll know the cause.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If it is under Windows, i doubt this will ever work, because you have opened this file, and Windows will not allow you to delete any file when it is open or being accessed.
    It has nothing to do with Windows. His example is wrong because he's passing "remove" a file pointer when it takes a character pointer as an argument.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 01:59 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  3. fopen vs. _open (for BIG image files)
    By reversaflex in forum C Programming
    Replies: 3
    Last Post: 04-01-2007, 01:52 AM
  4. Permanently deleting files
    By Yasir_Malik in forum Windows Programming
    Replies: 1
    Last Post: 08-01-2006, 11:07 PM
  5. deleting files not in fstream?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2001, 02:36 PM

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