470,596 Members | 1,564 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

std::remove syntax

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!
Jul 19 '05 #1
4 12426
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!


Jul 19 '05 #2

"Christopher Armstrong" <ch************@adelphia.net> wrote in message
news:Zp*******************@news2.news.adelphia.net ...
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
Jul 19 '05 #3
Christopher Armstrong wrote in
news:Zp*******************@news2.news.adelphia.net :
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/re...io.html#remove

I've never heard of std::erase.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #4
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/
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Venkat | last post: by
2 posts views Thread by Clement RAMBACH | last post: by
18 posts views Thread by JKop | last post: by
6 posts views Thread by Arne Claus | last post: by
15 posts views Thread by Andrew Maclean | last post: by
3 posts views Thread by craigbennett | last post: by
3 posts views Thread by groups | last post: by
13 posts views Thread by arnuld | last post: by
26 posts views Thread by Brad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.