Click here to Skip to main content
11,790,904 members (65,974 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++ C Linux Ubuntu
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?

char str[200];
        scanf("%80[^\r\n]", str);
Posted 6-Feb-13 3:06am
Quirkafleeg at 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 at 6-Feb-13 11:23am
yes i want any result from chdir
Quirkafleeg at 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

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

i also try this thing at it gives segmentation fault
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);
Quirkafleeg at 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 at 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)

  Print Answers RSS
0 OriginalGriff 410
1 ppolymorphe 385
2 Richard MacCutchan 118
3 Jochen Arndt 80
4 Maciej Los 70
0 OriginalGriff 1,329
1 Maciej Los 835
2 ppolymorphe 780
3 KrunalRohit 686
4 CPallini 661

Advertise | Privacy | Mobile
Web04 | 2.8.1509028.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100