Skip to content
Advertisement

C — using chdir() function

I’m trying to use chdir() function but can’t work it out.

I’m reading from user and find out if he is using “cd”. I always get an error. What am I doing wrong?

Code:

JavaScript

Advertisement

Answer

If the line being entered is something like:

JavaScript

then the directory starts at offset 3, not 2. In addition, fgets usually gives you a line with a newline character at the end so you’ll want to remove that as well, such as:

JavaScript

You should probably be tokenising the input a little more intelligently, a shell like bash (for example) has rather complex rules.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement