By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,232 Members | 1,719 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,232 IT Pros & Developers. It's quick & easy.

File Move Programmatically

P: n/a
Hi,

Would like to know how to move a file in c++ programmatically.Something like
File.Move in .net environment.

I am not looking at
Invoking the 'move' command through the 'System'.
Or
Looking at creating the file and copying the contents and deleting the same.

Cheers
Supreeth
Jul 22 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
supreeth wrote:

Hi,

Would like to know how to move a file in c++ programmatically.Something like
File.Move in .net environment.

I am not looking at
Invoking the 'move' command through the 'System'.
Or
Looking at creating the file and copying the contents and deleting the same.


The later is exactly the way to go.
Write the function once and add it to your toolkit library.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

P: n/a
Karl Heinz Buchegger wrote:

supreeth wrote:

Hi,

Would like to know how to move a file in c++ programmatically.Something like
File.Move in .net environment.

I am not looking at
Invoking the 'move' command through the 'System'.
Or
Looking at creating the file and copying the contents and deleting the same.


The later is exactly the way to go.
Write the function once and add it to your toolkit library.


That is: in standard C++

Your platform may contain a function to do that. Consult your
documentation or ask in a newsgroup dedictaed to your platform.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

P: n/a
"supreeth" <su******@sjce.net> writes:
Would like to know how to move a file in c++ programmatically.
Something like File.Move in .net environment.


I have not idea what `File.Move' in the .net environment is, but this is
probably what you're looking for:

#include <cstdio>
int main (void)
{
std::rename ("old_name", "new_name");
return 0;
}

(Error checking omitted for brevity.)

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Jul 22 '05 #4

P: n/a
Karl Heinz Buchegger <kb******@gascad.at> wrote:
I am not looking at
Invoking the 'move' command through the 'System'.
Or
Looking at creating the file and copying the contents and deleting the same.
The later is exactly the way to go.


Why is that? What is wrong with using 'std::rename()'?
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #5

P: n/a
Dietmar Kuehl wrote:

Karl Heinz Buchegger <kb******@gascad.at> wrote:
I am not looking at
Invoking the 'move' command through the 'System'.
Or
Looking at creating the file and copying the contents and deleting the same.

The later is exactly the way to go.


Why is that? What is wrong with using 'std::rename()'?


Aehm. nothing.
I forgot about it.
Thanks for the reminder.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #6

P: n/a
supreeth wrote:

Would like to know how to move a file in c++ programmatically.Something
like File.Move in .net environment.

I am not looking at
Invoking the 'move' command through the 'System'.
Or
Looking at creating the file and copying the contents and deleting the
same.

Cheers
Supreeth


You can use rename(2).. I dont know if you have something specific to .NET
but rename(2) is ANSI.

look this code:

-----------------------------------------------------------------

#include <fstream>
#include <cstdio>
#include <cstring>

using namespace std;

class myfstream : public fstream
{
private:
char *path;
public:
myfstream (const char *p, fstream::openmode m) {
int len = strlen (p);
path = new char[len + 1];
strncpy (path, p, len + 1);
open (path, m);

}
void rename (const char *npath)
{
std::rename (path, npath); // here we use rename (2);
delete [] path;
int len = strlen (npath) + 1;
path = new char [len];
strncpy (path, npath, len);

}
virtual ~myfstream () { delete[] path; }
};

int
main ()
{
myfstream outf ("tmp.dat", ios::out);
outf << "hi this file is called tmp.dat" << endl;
outf.rename ("otro.dat");
outf << "and now I have changed my mind this is otro.dat" << endl;
outf.close ();
return 0;
}
------------------------------------------------------------------
Jul 22 '05 #7

P: n/a

"Roberto Díaz" <rd********************@yahoo.es> wrote in message
You can use rename(2).. I dont know if you have something specific to .NET
but rename(2) is ANSI.


Just curious...what's the (2) indicate?

-Howard
Jul 22 '05 #8

P: n/a
Howard wrote:
You can use rename(2).. I dont know if you have something specific to
.NET but rename(2) is ANSI.
Just curious...what's the (2) indicate?


A UNIXism: it indicates that 'rename()' is documented in manual
section 2 which is the section for system calls. (1) would refer
to programs, (3) to library functions which are not system calls,
(4) to special files, (5) to file formats, (6) to games,
(7) to miscellaneous, (8) to system administration. Some flavors
of UNIX also add letters to indicate libraries, etc.

All this is, of course, not about C++...
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #9

P: n/a
On 2 Jun 2004 06:27:45 -0700, di***********@yahoo.com (Dietmar Kuehl)
wrote in comp.lang.c++:
Karl Heinz Buchegger <kb******@gascad.at> wrote:
I am not looking at
Invoking the 'move' command through the 'System'.
Or
Looking at creating the file and copying the contents and deleting the same.

The later is exactly the way to go.


Why is that? What is wrong with using 'std::rename()'?


Assuming the OP's use of the term "move a file" has the commonly
associated meaning of making its presence appear in a different
location in a file system than it did originally, there is absolutely
nothing in the C or C++ standards that requires or guarantees that
rename() will do this.

On some common desk top platforms, rename() may do so in some cases,
but often can't in others, such as when the source and destination
locations are on different physical devices or network locations.

In the general case this can't be done using standard C++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #10

P: n/a
On Wed, 02 Jun 2004 11:57:36 +0200, Martin Dickopp
<ex****************@zero-based.org> wrote in comp.lang.c++:
"supreeth" <su******@sjce.net> writes:
Would like to know how to move a file in c++ programmatically.
Something like File.Move in .net environment.


I have not idea what `File.Move' in the .net environment is, but this is
probably what you're looking for:

#include <cstdio>
int main (void)
{
std::rename ("old_name", "new_name");
return 0;
}

(Error checking omitted for brevity.)

Martin


Assuming the OP's use of the term "move a file" has the commonly
associated meaning of making its presence appear in a different
location in a file system than it did originally, there is absolutely
nothing in the C or C++ standards that requires or guarantees that
rename() will do this.

On some common desk top platforms, rename() may do so in some cases,
but often can't in others, such as when the source and destination
locations are on different physical devices or network locations.

In the general case this can't be done using standard C++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #11

P: n/a
Jack Klein <ja*******@spamcop.net> wrote:
In the general case this can't be done using standard C++.


Of course, immediately terminating a program before it did anything at all
due to a lack of resources also constitutes conforming behavior of programs
created by a C++ compiler. ... or, put differently, the C++ standard does
not guarantee that a program created by a conforming implementation does
anything useful at all. Thus, there is actually nothing a C++ program can
do using standard C++.

Maybe this is not the best perspective on things? Sure, there are load of
things which can fail when trying to move file (on some systems, the file
being open causes the renaming to fail; on some systems 'rename()' may fail
to move between different partitions or devices; the destination may exist
or not readable; actually, there is no need for the existance of something
like a "file" at all; etc.). I doubt that "File.Move()" can cope with all
those errors...

If you want to move a file, 'rename()' is way to go. If this fails you
might want to try other approaches like copying and then removing (which
can, BTW, also be done using standard C++), etc.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.