Click here to Skip to main content
Rate this: bad
good
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);
       (chdir(str)
       
Posted 6-Feb-13 4:06am
Comments
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 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 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 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 6 Feb 2013
Copyright © CodeProject, 1999-2014
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