Hello,
Im a new in c++ and need help,
can anybody give me one example how to delete file/s from c:\
for example
delete c:\test.txt
thanks very much
Printable View
Hello,
Im a new in c++ and need help,
can anybody give me one example how to delete file/s from c:\
for example
delete c:\test.txt
thanks very much
Don't hurt my, dwks, if you're reading this ::
should do it, hopefully. There's a non-system function method which I can't remember. Nor can I remember the header for system. Possibly cstdlib ...Code:system ("delete C:\\test.txt");
Olé, 800 posts!!
is this a right code?
#include <iostream.h>
int main()
{
system ("delete C:\\test.txt");
return 0;
}
if yes that gives error system undeclared identifier
>> #include <iostream.h>
<iostream> would be better.
If text.txt was in the same folder, and if that system delete thing works, I don't see why it shouln't work.
also, #include <cstdlib>
AND USE CODE TAGS!!!
Which OS and compiler are you using?
Look for the appropriate API function rather than using system()
Eg
http://www.cppreference.com/stdio/remove.html
remove() is ANSI standard, so you'll almost certainly have it.
I won't, I'll quote you instead:Quote:
Originally Posted by twomers
The same goes for delete. :DQuote:
Originally Posted by twomers
Thanks, dwks, I'd forgotten all about that! But I did say there was a non-system way ;) so I wasn't being completely unhelpful :D I think I'll leave the ' though, but I'll color it red
what about _unlink? or is it only for borland? btw you will need fstream for that...
try remove()
Do you mean the STL algorithm remove()? Unfortunately that is for removing elements in a container, nothing to do with file handling.Quote:
Originally Posted by achy1729
>> Do you mean the STL algorithm remove()?
No. There is a standard C function called remove that was mentioned already by Salem with a link to a reference to it.
The reason this didn't compile was because system is in <cstdlib> (or <stdlib.h> if your compiler's really old). But don't use system, because it's unportable and, as twomers forget, dangerous. You sould use the remove function already posted.Quote:
Originally Posted by Arsench
[edit][/edit]Code:#include <iostream>
#include <string>
#include <cstdio>
int main(void) {
std::string filename;
std::cout << "Enter the name of the file to delete (blank to cancel): " << std::endl;
std::getline(std::cin, filename);
if(filename != "") {
if(std::remove(filename)) {
std::cout << "Error removing file " << filename << std::endl;
}
else {
std::cout << "File " << filename << " removed successfully" << std::endl;
}
}
return 0;
}
>> or <stdlib.h> if your compiler's really old
stdlib.h is standard and will work on all modern compilers. I'm guessing you meant that you can't use cstdlib if your compiler is really old.
>> std::remove(filename);
remove takes a C style string, you probably meant std::remove(filename.c_str());.
Yes, and the alternative is <stdlib.h>, which doesn't include namespace support. That's pretty much what I said, isn't it?Quote:
>> or <stdlib.h> if your compiler's really old
stdlib.h is standard and will work on all modern compilers. I'm guessing you meant that you can't use cstdlib if your compiler is really old.
Indeed I did. :)Quote:
>> std::remove(filename);
remove takes a C style string, you probably meant std::remove(filename.c_str());.