0

i am building a program that copies directories to each other, but i can only get it to work hard coded.

i want the directory to be input by the user via a command line argument.

i have used

char    SrcPath[]    = argv[1]; 

however when i hard code it like

char SrcPath[] = "home/user/Desktop/cat"; it works fine. 

but this doesn't work, can anybody explain why / suggest a way to do this? and is there a special way that directories have to be input when used in a CLI?

James T. Smith
  • 410
  • 2
  • 9
juzuze
  • 13
  • 2
  • 1
    What is the error you get when you try to get the directory from the command line? – Irisshpunk Apr 23 '15 at 02:16
  • i just get my "cannot open directory" message that occurs when there's an error with finding the directory – juzuze Apr 23 '15 at 02:23
  • I think everyone was assuming but never mentioned: You understand that "home/user/Desktop/cat1" is a relative path and not an absolute path, right? Unless that directory is reachable from where the program start(at root "/"), then you can't change directory into it. – James T. Smith Apr 24 '15 at 11:43

2 Answers2

3

Making the change to char *SrcPath = argv[1]; should work. Pointers and arrays are different things!

James T. Smith
  • 410
  • 2
  • 9
  • You could point here for elaboration: http://stackoverflow.com/questions/1335786/c-differences-between-char-pointer-and-array – Irisshpunk Apr 23 '15 at 03:27
  • thank you for your response but i am still getting the same problem aOpenDir = opendir(SrcPath); if(!aOpenDir) printf("\n ERROR! Directory can not be open"); and when i try to execute the program i am entering ./a.out "home/user/Desktop/cat1" would this perhaps be the problem? – juzuze Apr 24 '15 at 00:29
0

argv[] is an array of char pointers, therefore when you use argv[1] you are obtaining the second item in the array which is a char pointer. As James suggested if you want to store the value of argv[1] which is a memory address you have to use a identical type which in this case is a char *.

If you need to save the directory path for any sort of processing or manipulation, you will need to do something like this to store the command line argument inside a char array.

char srcPath[100];
int i;
for (i = 0; argv[1][i] != '\0'; i++) {
    srcPath[i] = argv[1][i];
}

array names are pointers so using the subscript [] dereferences it. The same can be said for char arrays which is what is stored inside of argv[]

Anatzum
  • 79
  • 8
  • thanks so much for the response, but i am still getting the same error. – juzuze Apr 24 '15 at 00:26
  • aOpenDir = opendir(SrcPath); if(!aOpenDir) printf("\n ERROR! Directory can not be open"); and when i try to execute the program i am entering ./a.out "home/user/Desktop/cat1" would this perhaps be the problem? – juzuze Apr 24 '15 at 00:28
  • I would need to see more of the source file to see what's going on. Is opendir returning NULL? opendir is suppose to return a pointer stream to the opened directory and NULL if an error occurs – Anatzum Apr 24 '15 at 00:59
  • i figured out the issue, the code was right the input i was using was wrong. thank you so much for your answet – juzuze Apr 24 '15 at 06:34