File Move Programmatically

  • Thread starter Karl Heinz Buchegger
  • Start date
K

Karl Heinz Buchegger

supreeth said:
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.
 
K

Karl Heinz Buchegger

Karl said:
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.
 
S

supreeth

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
 
M

Martin Dickopp

supreeth said:
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
 
R

Roberto =?ISO-8859-15?Q?D=EDaz?=

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::eek:penmode 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::eek:ut);
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;
}
------------------------------------------------------------------
 
H

Howard

Roberto Díaz said:
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
 
D

Dietmar Kuehl

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++...
 
J

Jack Klein

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++.
 
J

Jack Klein

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/~ajo/docs/FAQ-acllc.html
 
D

Dietmar Kuehl

Jack Klein said:
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.
 

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
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top