Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code and i want to change my working directory what is the problem in my code or is there is any other way to change directory?

C#
char str[200];
        scanf("%80[^\r\n]", str);
       (chdir(str)
Posted
Comments
Quirkafleeg 6-Feb-13 10:04am    
What is the problem? You should get back 0 or -1 from chdir... then use errno to find out what the actual error code should be. This will give an idea as to what is happening.
FatimaAsif 6-Feb-13 11:23am    
yes i want any result from chdir
Quirkafleeg 8-Feb-13 9:40am    
OK - You should set a result, e.g. int result = chdir(str) and check the value of errno if result is -1. The errno is just another integer that you can output to the console. For more detail on which error values are returned, consult http://linux.die.net/man/2/chdir

1 solution

i also try this thing at it gives segmentation fault
C#
char str1[400];
        char str[200];
        char dirc[200];
        if(getcwd(dirc, sizeof(dirc)) != NULL){
            scanf("%5[^\r]", str);
            str1[400] = sprintf("%s %s", dirc, str);
        }
        chdir(str1);
 
Share this answer
 
Comments
Quirkafleeg 6-Feb-13 10:06am    
Two problems:
1) You cannot set str1[400] - str1 is an array from 0 to 399,
2) You cannot set a character from an entire string - you need to use strcpy instead
H.Brydon 6-Feb-13 13:42pm    
3) Also, don't update your problem as a solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900