std::remove syntax

Discussion in 'C++' started by Chandra Shekhar Kumar, Jun 26, 2003.

  1. what u need is erase-remove idiom like:
    v.erase(remove(v.begin(), v.end()), v.end())
    this will do for yr case..

    Christopher Armstrong wrote:

    > Hello! I'm trying to write a part of a program that will remove all files in
    > its directory. I have tried the std::remove feature of the standard library,
    > but I don't know its syntax. Also, what's the difference between std::remove
    > and std::erase? Thanks for your time!
    Chandra Shekhar Kumar, Jun 26, 2003
    #1
    1. Advertising

  2. Hello! I'm trying to write a part of a program that will remove all files in
    its directory. I have tried the std::remove feature of the standard library,
    but I don't know its syntax. Also, what's the difference between std::remove
    and std::erase? Thanks for your time!
    Christopher Armstrong, Jun 27, 2003
    #2
    1. Advertising

  3. "Christopher Armstrong" <> wrote in message
    news:ZpPKa.6330$...
    > Hello! I'm trying to write a part of a program that will remove all files

    in
    > its directory. I have tried the std::remove feature of the standard

    library,
    > but I don't know its syntax. Also, what's the difference between

    std::remove
    > and std::erase? Thanks for your time!
    >


    remove(name_of_file_to_delete);

    there is no std::erase function, where are you getting your information
    from?

    john
    John Harrison, Jun 27, 2003
    #3
  4. Christopher Armstrong wrote in
    news:ZpPKa.6330$:

    > Hello! I'm trying to write a part of a program that will remove all
    > files in its directory. I have tried the std::remove feature of the
    > standard library, but I don't know its syntax. Also, what's the
    > difference between std::remove and std::erase? Thanks for your time!
    >
    >

    This is the declaration of std::remove:

    int remove(const char *filename);

    its in <stdio.h> if you want it in the global namespace, or
    in <cstdio> if you want it in namespace std. To use it do:

    #include <cstdio>
    int main()
    {
    std::remove("filename.ext");
    }

    Here's some online docs:

    http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=stdio.html#remove

    I've never heard of std::erase.

    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jun 27, 2003
    #4
  5. Rob Williscroft wrote in news:Xns93A741BBB442EukcoREMOVEfreenetrtw@
    195.129.110.200:

    > Christopher Armstrong wrote in
    >


    Please respond to the newsgroup. That way when I talk nonsense
    somebody smarter can correct me.

    > Subject: std::remove Question


    > Rob, the following doesn't seem to work:
    > std::remove("*.*");


    std::remove() doesn't take wild cards. Unfortunatly there is no
    Standard C++ way to do what you want.

    You need to find a way of enumerating all the files you want
    to delete and then pass them one by one to std::remove().

    Your compiler most likely comes with some (non-standard)
    libraray code to do this. Some possibilities are:

    opendir()/readdir() - *nix (posix) systems and all the windows
    compilers I've seen.

    findfirst()/findnext() - dos/windows.

    A hack using std::system() (windows):

    #include <iostream>
    #include <fstream>
    #include <cstdio>
    #include <cstdlib>
    #include <string>
    #include <vector>

    std::vector< std::string > my_dirlist(std::string const &folder = ".")
    {
    std::vector< std::string > result;

    /* The argument to std::system() is platform dependant.
    This one will work for windows systems,
    on *nix you would use ls.

    If your platform doesnt have a directory listing
    command ('dir' below) and support redirection (the >)
    you're not going to get this hack to work.
    */
    std::system(("dir /B /A-D \"" + folder + "\" > 1.txt").c_str());

    std::ifstream ifs("1.txt");
    std::string temp;

    while (std::getline(ifs, temp))
    {
    if (temp.size() && temp != "1.txt")
    {
    result.push_back(temp);
    }
    }
    ifs.close();
    std::remove("1.txt");

    return result;
    }



    int main()
    {
    using namespace std;
    vector< string > strings = my_dirlist();

    vector< string >::iterator ptr, end;

    ptr = strings.begin();
    end = strings.end();

    for (;ptr != end; ++ptr)
    {
    cout << "removing: " << *ptr << endl;
    /*
    std::remove(ptr->c_str());
    */
    }
    }

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
    Rob Williscroft, Jun 27, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page