| | |
How to get the current working directory
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2007
Posts: 49
Reputation:
Solved Threads: 0
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.
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.
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.
•
•
Join Date: Aug 2007
Posts: 49
Reputation:
Solved Threads: 0
Ok my first problem is solved now my code is
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.
c++ Syntax (Toggle Plain Text)
char *path=NULL; size_t size; path=getcwd(path,size); 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.
•
•
•
•
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.
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.
You could use a stringstream to add a string to a char[]. It would look something like this (untested):
There are alot of other ways to do this.
-Niek
c Syntax (Toggle Plain Text)
#include <sstream> #include <iostream> using namespace std; int main(void) { char path[] = "c:\\test\\"; string file = "default.txt"; stringstream complete; complete << path << file; cout << complete.str() << endl; cin.get(); return 0; }
-Niek
Last edited by niek_e; Jan 9th, 2008 at 7:02 am.
•
•
Join Date: Dec 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
Ok my first problem is solved now my code is
c++ Syntax (Toggle Plain Text)
char *path=NULL; size_t size; path=getcwd(path,size); 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...
C++ Syntax (Toggle Plain Text)
#include <unistd.h> // getcwd() definition #include <sys/param.h> // MAXPATHLEN definition int main(int argc, char* argv[]) // Always write main() this way! {<blockquote>// Here's a way to use getcwd() char path1[MAXPATHLEN]; // This is a buffer for the text getcwd(path1, MAXPATHLEN); // The return value is redundant, but can be useful. // And here's one way to get to your program's path. // This is sometimes not the initial cwd, and this may not work // portably (because Windows uses '\\' and Linux uses '/'). string path2 = argv[0]; /* Copy the first command-line argument, which is the execution line of the program */ path2 = path2.substr(0, path2.find_last_of('/')); // Remove app name from string // Now, if you want to do more stuff... char file1[MAXPATHLEN]; strcpy(file1, path1); strcat(file1, "/default.txt"); // or... string file2 = path2; file2 += "/default.txt"; return 0;</blockquote>}
Some remarks to the previous post:
1. Alas,
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...
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. 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...
•
•
Join Date: Dec 2008
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
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...
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.
![]() |
Similar Threads
- ambiguous part 3 (C)
- Router not working (Network Security)
- File doesn't upload into the directory I specified. Need help! (PHP)
- Setting directory for opening files (Python)
- Directory and folder (C++)
- find a specific directory (Java)
- Solaris 5.8-Unidetified file (*nix Software)
- C++ Relcat/Attrcat catalog help (C++)
Other Threads in the C++ Forum
- Previous Thread: sort function questions
- Next Thread: My Triangle doesn't show?
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






