Delete known files

This is a discussion on Delete known files within the C++ Programming forums, part of the General Programming Boards category; Hello, Im a new in c++ and need help, can anybody give me one example how to delete file/s from ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    Angry Delete known files

    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

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Don't hurt my, dwks, if you're reading this ::

    Code:
    system ("delete C:\\test.txt");
    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 ...

    Olé, 800 posts!!

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    2
    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

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> #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!!!

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    remove() is ANSI standard, so you'll almost certainly have it.

    Quote Originally Posted by twomers
    Don't hurt my, dwks, if you're reading this ::
    Code:
    system ("delete C:\\test.txt");
    I won't, I'll quote you instead:
    Quote Originally Posted by twomers
    Read the FAQ about using cin.get() and cin.ignore() to avoid using system("pause") commands, unless you have to. The reason why you should try to avoid using system commands is because, apparently, some people like to hack into computers, and make batch files, called pause or cls or whatever, so when you system ("pause"), you inadvertently execute the bat file, and the most popular conclusion is that your entire hard drive is deleted.
    The same goes for delete.
    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.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    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

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    29
    what about _unlink? or is it only for borland? btw you will need fstream for that...

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    try remove()

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by achy1729
    try remove()
    Do you mean the STL algorithm remove()? Unfortunately that is for removing elements in a container, nothing to do with file handling.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Quote Originally Posted by Arsench
    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
    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.
    [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;
    }
    [/edit]
    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.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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());.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    >> 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.
    Yes, and the alternative is <stdlib.h>, which doesn't include namespace support. That's pretty much what I said, isn't it?

    >> std::remove(filename);
    remove takes a C style string, you probably meant std::remove(filename.c_str());.
    Indeed I did.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. How to Delete files
    By wyvern in forum C++ Programming
    Replies: 6
    Last Post: 12-01-2005, 12:42 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