• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

delete files in C++

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Bmxpunk86pl

Member
Joined
Sep 7, 2001
Location
CT/Poland
how do i delete files in C++?

do i do this:

remove ("C:\\test.txt");

but when i do that, it will not delete the file. I dont get any errors but it just doesnt delete the file.


p.s. there is more to the program, this is just a snippet.

thanks,
Adam
 
there is a better way that I can't think of but you can execute a system command "Del xxx.tmp". You'll ahve to look up the acutal function, I can't think of it off the top of my head. It's something like "syscom('Del xxxx.tmp')". But Like I said it's been a while.
 
i get an error saying that:

7 untitled1.cpp implicit declaration of function `int system(...)'

i wrote this:

#include <stdio.h>
#include <fstream.h>
#include <iostream.h>

int main(int argc, char *argv[])
{
system ("del C:\\test.txt");
return 0;
}

anyideas?

Thanks,
Adam
 
you are not including the header file that contains the definition for the function system()...

I cannot remember what it is right now.... But, that is your problem.

*edit*
Ahhh, I remeber now, it is the "process.h"
 
alright i did it but it doesnt delete the file for some reason. Here is my program:

#include <stdio.h>
#include <process.h>
#include <stdlib.h>


int main(int argc, char *argv[])
{
system ("del C:\\test.txt");
return 0;
}
 
maybe i'm just nuts, but

do you really need both slashes "c:\\test.txt"???
try cutting it down to one: "c:\test.txt"...

Only thing i could think of.
 
no because of what C++ uses the '\' for, it will not work. Because C++ was made under Unix and unix does not use "\". C++ uses \n, \t, so it though that i was using that. So u gotta use the double back slashed. Also i got it to work, so now i can delete files.
 
no because of what C++ uses the '\' for, it will not work. Because C++ was made under Unix and unix does not use "\". C++ uses \n, \t, so it though that i was using that.

You have to use the double back slashes so the compiler knows you're not trying to use an escape character, ie \n \t . I don't think it has anything to do with being designed for Unix.
 
Not exactly what you said before. I guess I should have been clearer. Since C++ defines the backslash as an escape character, there needed to be a way to instruct the languages you really just want a backslash. The obvious solution is to leave the backslash as an escape character and follow it by the symbol \ which is C++ code for backslash. It has nothing to do with any OS, it's just a way to allow backslashes when backslashes are defined by the language as an escape character.

I know this is nit picky, but the syntax has nothing to do with the platform.
 
wow I found the anwser without making my own thread for once! Now I just need a little more help with deleting files. In my program I let the user pick the name of the file and store it int char array [30];. How can I delete this file later. I dont know the exact c: path because the file is created in the same folder as where the program is executed. Any help would be greatly appreciated!
 
Back