using the cp command in a program in os x

This is a discussion on using the cp command in a program in os x within the C Programming forums, part of the General Programming Boards category; ok i have this program, i want to have it so the user types in the path of a file, ...

  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,069
    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,069
    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,006
    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,069
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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

Page 1 of 2 12 LastLast
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, 10: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, 06:49 PM
  4. How to program a OS
    By iwod in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2001, 07:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21