How to get the current working directory

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

How to get the current working directory

 
0
  #1
Jan 9th, 2008
Hi
What is require is to get the path of current working directory.I m working on windows-XP.
Is any function avaliable in c/c++ i could find on net
#include <unistd.h>
char *getcwd(char *buf, size_t size);

But not working i don't know why. Can u provide small code of how to use this function or any other function which can do the above task in a better way.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,879
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: How to get the current working directory

 
0
  #2
Jan 9th, 2008
> But not working..

What is not working? How did you use the command?

> Can U provide small code...

Yup, it took me about 10 seconds on google

regards Niek
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to get the current working directory

 
0
  #3
Jan 9th, 2008
Microsoft compilers do not use unistd.h -- that's for *nix. And Microsoft put an underscore in front of the name, so its _getcwd().. The link contains example code too.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Re: How to get the current working directory

 
0
  #4
Jan 9th, 2008
Ok my first problem is solved now my code is

  1. char *path=NULL;
  2. size_t size;
  3. path=getcwd(path,size);
  4. cout<<"\n current Path"<<path;

This is working fine. Now what i want is to add string to this path. Say currently path is C:\MyProject. Now i want to add \default.txt to the value of path so that it become C:\MyProject\default.txt . As this function returns a char * i'm not able to do this. Kindly help.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,879
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: How to get the current working directory

 
0
  #5
Jan 9th, 2008
Originally Posted by Ancient Dragon View Post
Microsoft compilers do not use unistd.h -- that's for *nix. And Microsoft put an underscore in front of the name, so its _getcwd().. The link contains example code too.
Hmm, I've tried the code on my Windows Machine with VC2005 and it compiles fine ? Not even warnings or whatever..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to get the current working directory

 
0
  #6
Jan 9th, 2008
use setenv(), but it only works for the current process. When that process ends the new environment variable is reset back to its original state. The only way to make it permanent in the system is to change it via control panel.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,879
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: How to get the current working directory

 
0
  #7
Jan 9th, 2008
Originally Posted by tonyaim83 View Post
Now what i want is to add string to this path.
You could use a stringstream to add a string to a char[]. It would look something like this (untested):
  1. #include <sstream>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main(void)
  7.  
  8. {
  9. char path[] = "c:\\test\\";
  10. string file = "default.txt";
  11.  
  12. stringstream complete;
  13. complete << path << file;
  14. cout << complete.str() << endl;
  15. cin.get();
  16. return 0;
  17. }
There are alot of other ways to do this.

-Niek
Last edited by niek_e; Jan 9th, 2008 at 7:02 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 4
Reputation: Jonny D is an unknown quantity at this point 
Solved Threads: 0
Jonny D Jonny D is offline Offline
Newbie Poster

Re: How to get the current working directory

 
0
  #8
Dec 6th, 2008
Originally Posted by tonyaim83 View Post
Ok my first problem is solved now my code is

  1. char *path=NULL;
  2. size_t size;
  3. path=getcwd(path,size);
  4. cout<<"\n current Path"<<path;

This is working fine. Now what i want is to add string to this path. Say currently path is C:\MyProject. Now i want to add \default.txt to the value of path so that it become C:\MyProject\default.txt . As this function returns a char * i'm not able to do this. Kindly help.

Just to clear things up for future readers...

  1. #include <unistd.h> // getcwd() definition
  2. #include <sys/param.h> // MAXPATHLEN definition
  3.  
  4. int main(int argc, char* argv[]) // Always write main() this way!
  5. {<blockquote>// Here's a way to use getcwd()
  6. char path1[MAXPATHLEN]; // This is a buffer for the text
  7. getcwd(path1, MAXPATHLEN);
  8.  
  9. // The return value is redundant, but can be useful.
  10.  
  11. // And here's one way to get to your program's path.
  12. // This is sometimes not the initial cwd, and this may not work
  13. // portably (because Windows uses '\\' and Linux uses '/').
  14. string path2 = argv[0]; /* Copy the first command-line argument,
  15.   which is the execution line of the program */
  16. path2 = path2.substr(0, path2.find_last_of('/')); // Remove app name from string
  17.  
  18. // Now, if you want to do more stuff...
  19. char file1[MAXPATHLEN];
  20. strcpy(file1, path1);
  21. strcat(file1, "/default.txt");
  22. // or...
  23. string file2 = path2;
  24. file2 += "/default.txt";
  25.  
  26. return 0;</blockquote>}
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: How to get the current working directory

 
0
  #9
Dec 6th, 2008
Some remarks to the previous post:
1. Alas, argv[0] IS NOT a path of exe module.
The C standard (5.1.2.2.1 Program Startup):
If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment.
The program name is not the same thing as an exe path!
2. The exe directory does not bear a relation to the current working directory...
3. No platform-independent methods to get the current working directory. The getcwd function is not C and C++ standard library function...
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 4
Reputation: Jonny D is an unknown quantity at this point 
Solved Threads: 0
Jonny D Jonny D is offline Offline
Newbie Poster

Re: How to get the current working directory

 
0
  #10
Dec 7th, 2008
Originally Posted by ArkM View Post
Some remarks to the previous post:
1. Alas, argv[0] IS NOT a path of exe module.
The C standard (5.1.2.2.1 Program Startup):

The program name is not the same thing as an exe path!
2. The exe directory does not bear a relation to the current working directory...
3. No platform-independent methods to get the current working directory. The getcwd function is not C and C++ standard library function...
"If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. "

Is your reference the c0x standard? It's not even implemented yet... It's called the c0x standard because it is meant to be released some year this decade (now it must be 2009). For most people, portability between Windows, MacOS, and Linux are enough. On those OSes, getcwd() works and argv[0] works (at least with GCC) to give you the full command line call to your executable. Don't argue until you try the above code... What else is the "program name" except what the OS sees (i.e. what it needs to know in order to run your program)? You'll be hard-pressed to find a counter-example to this functionality, though I welcome one.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC