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
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
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
remove() is ANSI standard, so you'll almost certainly have it.
I won't, I'll quote you instead:Originally Posted by twomers
The same goes for delete.Originally Posted by twomers
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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 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.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.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; }
Last edited by dwks; 09-06-2006 at 09:14 AM.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
>> 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?>> 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.>> std::remove(filename);
remove takes a C style string, you probably meant std::remove(filename.c_str());.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.