Connecting Tech Pros Worldwide Help | Site Map

std::remove syntax

Christopher Armstrong
Guest
 
Posts: n/a
#1: Jul 19 '05
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
Guest
 
Posts: n/a
#2: Jul 19 '05

re: std::remove syntax


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:
[color=blue]
> 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![/color]

John Harrison
Guest
 
Posts: n/a
#3: Jul 19 '05

re: std::remove syntax



"Christopher Armstrong" <chrisarmstrong@adelphia.net> wrote in message
news:ZpPKa.6330$Hw.4953969@news2.news.adelphia.net ...[color=blue]
> Hello! I'm trying to write a part of a program that will remove all files[/color]
in[color=blue]
> its directory. I have tried the std::remove feature of the standard[/color]
library,[color=blue]
> but I don't know its syntax. Also, what's the difference between[/color]
std::remove[color=blue]
> and std::erase? Thanks for your time!
>[/color]

remove(name_of_file_to_delete);

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

john


Rob Williscroft
Guest
 
Posts: n/a
#4: Jul 19 '05

re: std::remove syntax


Christopher Armstrong wrote in
news:ZpPKa.6330$Hw.4953969@news2.news.adelphia.net :
[color=blue]
> 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!
>
>[/color]
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/
Rob Williscroft
Guest
 
Posts: n/a
#5: Jul 19 '05

re: std::remove syntax


Rob Williscroft wrote in news:Xns93A741BBB442EukcoREMOVEfreenetrtw@
195.129.110.200:
[color=blue]
> Christopher Armstrong wrote in
>[/color]

Please respond to the newsgroup. That way when I talk nonsense
somebody smarter can correct me.
[color=blue]
> Subject: std::remove Question[/color]
[color=blue]
> Rob, the following doesn't seem to work:
> std::remove("*.*");[/color]

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/
Closed Thread