473,394 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 12673
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Venkat | last post by:
Hi All, Currently i guess rename and remove are not supported using fstream. How do i rename or remove a file? regards, Venkat
2
by: Clement RAMBACH | last post by:
Hi, here is my problem: I have a std::vector< A* >. This vector contains pointers to the objects A i create (lets say about 4000 items). I do this several times in my application, and at the...
18
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative...
6
by: Arne Claus | last post by:
Hi If've just read, that remove() on a list does not actually remove the elements, but places them at the end of the list (according to TC++STL by Josuttis). It also says, that remove returns a...
15
by: Andrew Maclean | last post by:
I guess this problem can be distilled down to: How do I search through a string, find the first matching substring, replace it, and continue through the string doing this. Can replace_if() be used...
3
by: craigbennett | last post by:
Hi, I am trying to use std::remove to delete a file and have been encountering some problems. My goal is to pass std::remove a std::string and remove the file of that name, for example. ...
3
by: groups | last post by:
This small piece of code is troubling me.. is there anything wrong with it?? After calling this method the contents ot the input vector are completely screwed.. (CCountedCIG_CountZero() applies...
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
26
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.