All about programming in GNU/LINUX

chdir and getcwd system calls

#include<unistd.h>
int main()
{
    char *buf,*ch;
    buf=malloc(30*sizeof(int));

    ch=malloc(30*sizeof(int));
    ch=”/home/karthic/codes”;//replace this with a valid path on your system
    chdir(ch);//changing the current directory
    buf=getcwd(buf,120);//getcwd is the system call which writes the current path into the string
    /*the second argument to the call is the no.of.bytes available in the string ,
    which indicates the max.no.of.bytes hat is to be written into the string */

    printf(“\nThe current working directory is :%s “,buf);
    return 0;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s