std::remove syntax

  • Thread starter Chandra Shekhar Kumar
  • Start date
C

Chandra Shekhar Kumar

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

Christopher Armstrong

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!
 
J

John Harrison

Christopher Armstrong said:
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
 
R

Rob Williscroft

Christopher Armstrong wrote in
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.
 
R

Rob Williscroft

Rob Williscroft wrote in 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top