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.