473,325 Members | 2,785 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,325 software developers and data experts.

File Move Programmatically

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
11 20019
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
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
"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
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
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
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

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

Similar topics

0
by: davidchildreth | last post by:
It would make my clients life easier if he could place attached file into an unbound object frame on a form. This activity causes bloat if there are many such insertions. I would like to...
6
by: Null Reference | last post by:
Anybody here who can explain or point me to a link ? I wish to create a blank MS Access DB file programmatically using C# . Thanks, nfs
2
by: Mat | last post by:
in my program options, users can reorder DB grid columns( in listbox) Then, i want to programmatically simulate col move event and reorder the columns. Help plz
0
by: bloggs | last post by:
I am writting an application that runs on an FTP server. The purpose of the app is to uncompress gzip files that are sent to the server and then move the file to another folder according to it's...
8
by: ILCSP | last post by:
Hello, I have a complete project in VB.NET (2003) with all the path to the folders where the files being used are. For example I have these variables: ' Setup Directory Paths ImportDir =...
0
by: crackajaxx | last post by:
I've hunted for a while so I'm hoping someone can shed some light on this problem for me. I have an application (not a web-app, in fact this is an installed service) that relies upon its...
2
by: Gerhard | last post by:
I have a .net application that I want to run in a DMZ, with the SQL Server and file system behind another firewall. Is there a secure way to get to files from my application, or would it be better...
5
balabaster
by: balabaster | last post by:
How do you programmatically shift focus to the next tab stop? i.e. I have a bunch of textboxes that all have their tab orders set in sequence. The user hits enter as they finish entering data in any...
3
by: Rishabh Indianic | last post by:
Hi, I am used VS 2005 with c# for developing window mobile application. When i create a cab file i add Primary Output and Content files from project. The content file contain image folder. When...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.