Thread: using the cp command in a program in os x

  1. #1
    Registered User peateargriffin's Avatar
    Join Date
    Apr 2006
    Posts
    11

    using the cp command in a program in os x

    ok i have this program, i want to have it so the user types in the path of a file, and then the programs copies that file to another specified location. i have this so far:

    Code:
    #include <stdio.h> 
    
    int main() 
    { 
       char orig[50]; 
       char dest[50]; 
           
       printf("where is the file you would like to copy? start with desktop, with a \"/\" between each foler. \n"); 
       scanf("%s",&orig); 
       printf("where would you like this file to be copied to? use the same format \n"); 
       scanf("%s",&dest); 
        
       system("cp ~/%s ~/%s", orig, dest); 
        
       printf("DONE!"); 
        
       return(0); 
    }

    the problem is it comes up with an error that says "users/peter/%s isnt a directory"
    how do i get it to recognize the string i have after it? or can i at all?

    thanks
    Last edited by peateargriffin; 04-06-2006 at 08:37 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    system doesn't take data specifiers in its argument. You have to strcat the whole thing together and send that to system().

    Also, use [CODE] tags.
    Sent from my iPad®

  3. #3
    Registered User peateargriffin's Avatar
    Join Date
    Apr 2006
    Posts
    11
    i thought i did use code tages...oops...and i dont know what strcat is, so either you could explain it briefly? or i can wait
    thanks

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    #include <stdio.h>
    #include <string.h>   // For string functions including strcat();
    
    int main() {
       char str1[13] = "Hello ";
       char str2[7] = "World!";
    
       strcat(str1,str2);     // strcat(a,b) appends b to a.
       printf("%s", str1);
    
       return 0;
    }
    
    /* Output:
       Hello World! */
    A bit more detail, for you. First off, system(), which by the way is actually in <stdlib.h>, is generally frowned upon. Secondly, scanf() isn't used for strings. Especially strings that may contain spaces. If the input was "C:/Documents And Settings/..." you'd end up with "C:/Documents". You should be using fgets() for strings.
    Last edited by SlyMaelstrom; 04-06-2006 at 08:50 PM.
    Sent from my iPad®

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by SlyMaelstrom
    Code:
    #include <stdio.h>
    #include <string.h>   // For string functions including strcat();
    
    int main() {
       char str1[7] = "Hello ";
       char str2[7] = "World!";
    
       strcat(str1,str2);     // strcat(a,b) appends b to a.
       printf("%s", str1);
    
       return 0;
    }
    
    /* Output:
       Hello World! */
    Hmm?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User peateargriffin's Avatar
    Join Date
    Apr 2006
    Posts
    11
    how would i use that though?

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Dave_Sinkula
    Hmm?
    Whoops... good call. I'm a boob.
    Sent from my iPad®

  8. #8
    Registered User peateargriffin's Avatar
    Join Date
    Apr 2006
    Posts
    11
    ok im lost....

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Boobies.

    Code:
    #include <stdio.h> 
    
    int main() 
    { 
       char orig[50]; 
       char dest[50];
       char stuff[100]; 
           
       printf("where is the file you would like to copy? start with desktop, with a \"/\" between each foler. \n"); 
       scanf("%s", orig); 
       printf("where would you like this file to be copied to? use the same format \n"); 
       scanf("%s", dest); 
    
       sprintf(stuff, "cp ~/%s ~/%s", orig, dest);
    
       system(stuff); 
       printf("DONE!"); 
        
       return(0); 
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Also, ~ is interpreted by the shell, not the cp command.

    Perform the substitution of ~ in the code itself - ideally passing to the cp command two absolute paths.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User peateargriffin's Avatar
    Join Date
    Apr 2006
    Posts
    11
    woah thanks everyone that helped. i cant try that now, but i will when i get home (school, always getting in the way)
    quick question, i asked this question at the ipodlinux forums, and they said the command was snprintf(blah blah) not sprintf(blah blah), which is it?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well snprintf() is preferable if you have it, but it is a C99 standard function. It is not a C89 function.

    It's safer, but you lose some portability (basically)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User peateargriffin's Avatar
    Join Date
    Apr 2006
    Posts
    11
    thanks you guys it worked, i wouldnt have been able to figure that out. thanks a bunch, ill be back!

    thanks

  14. #14
    Registered User
    Join Date
    Jul 2002
    Posts
    11
    Quote Originally Posted by Dave_Sinkula
    Hmm?
    No, he was correct before. str1 should be a size of 13 to allow Hello World!\0. Read the manual for strcat, char *s MUST be large enough to hold what it is being appended.

  15. #15
    Registered User peateargriffin's Avatar
    Join Date
    Apr 2006
    Posts
    11
    question, can you have more than one getchar in a program? cause everytime i have more than one getchar, with an if/else following, the if's and else's get ignored.
    here's teh code:
    Code:
    /*
     *  copy.c
     *  
     *
     *  Created by Peter Noone on 4/6/06.
     *  Copyright 2006 __MyCompanyName__. All rights reserved.
     *
     */
    
    #include <stdio.h>
    
    int main()
    {
    	char q;
    	char a;
    	char original[50];
    	char orig[50];
    	char file[10];
    	char dest[50];
    	char cp[100];
    		
    	printf("where is the file you would like to copy? \nif the file is on the desktop, type \"y\" \nif not, type \"n\" \n");
    	q=getchar();
    	if(q=='y')
    	{
    		printf("now type the file name.\n");
    		scanf("%s", &file);
    		sprintf(original,"~/desktop");
    		sprintf(orig,"desktop/%s",file);
    	}
    	
    	else
    	{	
    		printf(" type the full path of the file, start with \"/desktop,\" with a \"/\" \n between each foler. \n");
    		scanf("%s",&orig);
    	}
    	
    	if(q=='y')
    	{
    		printf("would you like the copy made to the same location? (y/n)\n");
    		a=getchar();
    		if(a=='y')
    		{
    			sprintf(dest,original);
    		}
    	}
    	else
    	{
    		printf("where would you like this file to be copied to?\n");
    		printf("type the full path of the destination. \n");
    		scanf("%s",dest);
    	}
    	
    	sprintf(cp,"cp ~/%s ~/%s", orig, dest);
    	
    	system(cp);
    	
    	printf("DONE!");
    	
    	return(0);
    }
    and here's what i get when i run it (it only works if i chose a file that was on the desktop. i.e. if i chose "y" at the first option)
    Code:
    new-host-2:/users/peter/desktop/other/programming Peter$ ./copy
    where is the file you would like to copy? 
    if the file is on the desktop, type "y" 
    if not, type "n" 
    y
    now type the file name.
    5g.scpt
    would you like the copy made to the same location? (y/n)
    cp: /Users/Peter/??.?: Invalid argument
    DONE!

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. How to program a OS
    By iwod in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2001, 08:20 PM