How to Delete files

This is a discussion on How to Delete files within the C++ Programming forums, part of the General Programming Boards category; imagine i make a program that needs to store a var in a separated file ( Auxiliar_file.txt ) and when ...

  1. #1
    Dragoon Lover wyvern's Avatar
    Join Date
    Jul 2005
    Location
    dragooncity
    Posts
    28

    How to Delete files

    imagine i make a program that needs to store a var in a separated file ( Auxiliar_file.txt ) and when i finish it i want the program to delet it.
    all i know is to erase whats inside but how i delete it ?

    REASON: i want to make a game and i will need few vars ( HP,MP,Points,EXP,LVL ... ) and when the user delelets the account ( in the game imagine 1-Generate caracter; 2-Load character ... see?? ) well whe the user wants to delete its character how i delete the files i created ?????
    http://img76.imageshack.us/img76/1808/expl7pb.png

    AN EXPLOSION DOESNT HAPPEN CUZ GOD WANTS, IT HAPPENS WHEN A SOMETHING "EXPLODES

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Do you want the program to delete the actual file or delete the information in it?
    Sent from my iPadŽ

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Do you want the program to delete the actual file or delete the information in it?
    Da dunha!:

    and when i finish it i want the program to delet it.
    all i know is to erase whats inside but how i delete it ?


    The easiest way would be with a call to the system; but that is not portable from OS to OS.

    For windows based:
    Code:
    	system("del myfile.txt");

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Yeah, yeah... some people have trouble phrasing exactly what they mean, and most people don't like getting code that deletes files that they don't actually want deleted.

    Anyway, that's not the best way to do this. As we all know making that call is bad.

    cstdio has a function called remove().
    Code:
    remove(filename);
    ...and this is portable. Just make sure you close the file before you attempt to remove it, otherwise this function fails.

    Be warned, this DELETES the file from the computer permenantly. If that's not what you wanted, then don't try it on any important files.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Never mind, just make sure your file is in the current directory or use the full path
    Last edited by Quantum1024; 12-01-2005 at 12:34 PM.

  6. #6
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    cstdio has a function called remove().
    Sweet!
    I always forget about all the cool things in the standard libraries! I be tha suck!

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    unlink() is also available on both MS-Windows and *nix. I think it is just another name for remove(), but *nix people like unlink() because that's how they remove files on the command-line -- I think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  4. why delete[] and delete?
    By dude543 in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2005, 10:55 PM
  5. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 10:17 PM

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