Jump to content
  • Advertisement
Sign in to follow this  
Ksingh30

C and Unix

This topic is 4273 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can someone help me out please. I need a string. so "char *mystring" and i need that string to be equal the current unix directoy how can i do that?

Share this post


Link to post
Share on other sites
Advertisement
The POSIX function getcwd() will give you the current working directory. Read the documentation before using it, as it can be unsafe.

Share this post


Link to post
Share on other sites

#include<unistd.h>

int main(void)
{
char mystring[100];

getcwd(mystring);

return 0;
}



The getcwd() function puts the name of the current working directory into a char *. Make sure to #include<unistd.h> and make sure mystring has enough space to hold the name. Type 'man 3 getcwd' on your terminal for more info.

Share this post


Link to post
Share on other sites
that didnt work.
I tried this

char* getpath()
{
char *T;
getcwd(T);
return T;
}

and I got the following errors:
error: too few arguments to function ‘char* getcwd(char*, size_t)’

I guess it also wants some size.

Share this post


Link to post
Share on other sites
That won't work either, erissian. The memory assigned to T is allocated locally, and will be deallocated when the call boundary is crossed (ie, when the function returns). You end up returning a pointer to memory that is no longer "valid."

bool getpath(char **out, int max_len)
{
return 0 == getcwd(*out, max_len);
}

If you can figure out how that code works, then you know enough to use getcwd safely. If not, you don't.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!