Click here to Skip to main content
13,591,515 members
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);
Posted 6-Feb-13 3:06am
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.
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
FatimaAsif 6-Feb-13 11:23am
yes i want any result from chdir

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 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

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

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