Click here to Skip to main content
12,953,937 members (42,591 online)
Rate this:
 
Please Sign up or sign in to 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?

char str[200];
        scanf("%80[^\r\n]", str);
       (chdir(str)
Posted 6-Feb-13 3:06am
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

Rate this: bad
 
good
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);
        }
        chdir(str1);
  Permalink  
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,464
CHill60 3,490
Maciej Los 3,123
ppolymorphe 2,020
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2017
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