Quantcast

Maximum PC

It is currently Mon Jul 28, 2014 11:08 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Two questions about file manipulation in C++
PostPosted: Wed Dec 10, 2008 8:55 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
I have two questions about file management in C++ :

Firstly, how can I copy a file to another directory. Should I use the System() command, or is there a better way?

Second, Is there any way to copy to another directory the executable itself, that is, the file currently running the commands? I cannot rely on the name of the file either, as this may be changed.

Thanks in advance for your help.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2008 4:41 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
in C++.NET
http://msdn.microsoft.com/en-us/library ... copy(VS.71).aspx

In normal C++
http://www.rohitab.com/discuss/index.ph ... t&p=126059

you should be able to copy any file... however, you do need to know the name of the specific file you wish to copy.

Use this to get the exe name?
http://blogs.msdn.com/brada/archive/200 ... 13620.aspx

then use the above method to copy it?


Top
  Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2008 5:22 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
argv[0] should hold the name of the application ... hopefully, that is the filename that you need.

I think that Crash covered the HOWTO pretty well.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2008 4:45 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
I put that into code, but I get an error on the CopyFile() function about the arguments I'm using. It's probably a n00bish error I know :)

Code:
#include <iostream>
#include <cstdlib>
#include "windows.h"

using namespace std;

int main(int *argc, char *argv[]) {
   cout << "Hi there!" << endl << endl;
   cout << argv[0];
   string exe_name = string(argv[0]);
   CopyFile(exe_name,"C:\\test.exe");
   cout << "Copy finished." << endl << endl;
   system("pause");
   return 0;
}


Top
  Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2008 10:32 pm 
Little Foot
Little Foot
User avatar

Joined: Mon Jul 28, 2008 1:39 pm
Posts: 123
UPDATE: recoded it in C#.NET and it worked fine.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SelfCopying
{
    class Program
    {
        static void Main(string[] args)
        {
            String exeName = Environment.GetCommandLineArgs()[0];
            if (exeName.Equals("C:\\test.exe"))
            {
                Console.WriteLine("This program has already been copied!");
                Console.WriteLine("Move it to another location, and then run it.");
                Console.WriteLine("Press <enter> to exit the program.");
            }
            else
            {
                Console.WriteLine(exeName + "\n\n");
                System.IO.File.Copy(exeName, "C:\\test.exe", true);
                Console.WriteLine("Copy successful.\n\nPress <enter> to exit.");
            }
            Console.ReadLine();
        }
    }
}



Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group