Thread: alternative copy.com ;-)

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    34

    Question alternative copy.com ;-)

    Hi again! Yesterday I tried something, I remember I found it very stupidly! I wanted to get a filename as an argument and then put it into a variable and put it into a system(); statement! So it got something like this:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(int argc, char *argv[])
    {
    if ( argc < 2)
    {
    printf("Usage copy [file1] [target]\n");
    return 0;
    }
    else
    {
    system("copy" argv[1] argv[2]);
    return 0;
    }
    Of course it didn't work because the statement system() does not allow this, but then I asked myself: How to make it diferent?
    And now I ask you! Could you help me...? Thanx! [email protected] !

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You need to concatenate your arguments into the string. This is easiest with strcat()

    Code:
    #include<string.h>
    .
    int PLENTYOFSPACE= 
    char buffer[PLENTYOFSPACE]
    strcpy(buffer,"copy ");
    strcat(buffer,argv[1]);
    strcat(buffer," ");
    strcat(buffer,argv[2]);
    system(buffer);
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    int main(int ArgCount, char* Args[])
    {
       char Buffer[1024] = {0};
       if(ArgCount >= 3)
       {
          strcpy(Buffer, "copy ");
          strcat(Buffer, Args[1]);
          strcat(Buffer, " ");
          strcat(Buffer, Args[2]);
          System(Buffer);
       }   
       return 0;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    That all looks pretty horrible.... why not write your own copy program and stop using that systen() function. It's easy enough, and has been done many times before.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. alternative for fflush(stdin)
    By Raison in forum C Programming
    Replies: 11
    Last Post: 04-13-2011, 04:58 AM
  2. ASP.NET alternative to gallery.menalto.com ???
    By gicio in forum C# Programming
    Replies: 0
    Last Post: 05-15-2005, 11:31 AM
  3. Alternative to fstream
    By golfinguy4 in forum Windows Programming
    Replies: 3
    Last Post: 07-03-2002, 01:51 AM