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.

> 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

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.

Ok my first problem is solved now my code is

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.

Hmm, I've tried the code on my Windows Machine with VC2005 and it compiles fine ? Not even warnings or whatever..

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.

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):

#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;
}

There are alot of other ways to do this.

Ok my first problem is solved now my code is

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...

#include <unistd.h>  // getcwd() definition
#include <sys/param.h>  // MAXPATHLEN definition

int main(int argc, char* argv[])  // Always write main() this way!
{
[INDENT]// 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;[/INDENT]
}

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...

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.

My first thoughts are, why have you dragged an 11 month old thread up! My next thoughs are:

Standards do NOT state that argv[0] is the filename. However it is considered good practice to make this the case. So if you are after true portability then you cannot rely on it. It is down to the OS whether or not the first argument passed is the filename. You may find that some even pass the path of the exectuable...making it even more unstandardized!

chris

Well..Then use strcat

file="\\default.txt"
if pathlength+filelength > limit
   return 1
strcat(Path,file);

My first thoughts are, why have you dragged an 11 month old thread up! My next thoughs are:

chris

All the DaniWeb threads I read are found through Google. This one happened to come up as the first result in a search, so I wanted to clarify the response, hence:
"Just to clear things up for future readers..."

By the way, I'm sorry if I sounded hostile in my last post. What I mean is that we should cite examples (implementations) over standards for practical reasons.

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.

Alas, all your statements are wrong. It was a quote from the CURRENT Standard (was adopted 10 years ago). I never said that getcwd function does not work IF IT'S IMPLEMENTED, but it's not standard function - that's all. MS VC++ exe module gets full path in argv[0] if it started from IDE but argv[0] contains file name only (without path and extension) when it started from command line window.
It's funny ;)...

That's better (except that "all" of my statements are wrong). I do wonder how it works in MS-DOS. Probably the same way. Either way, the code that I wrote works to bring you to your program's directory from the current working directory, which is what you would want. If you prefix file names with the path retrieved from argv[0], then you'll get the right file, even at the command line.

I don't know whether this would help, but I needed something along the lines of basename $PWD in linux and the following seems to work in both linux and DOS environs - it is based on a prior suggestion to this thread:

#include <unistd.h>
#include <sys/param.h>

std::string find_basename()
{
     char path1[MAXPATHLEN];
     getcwd(path1, MAXPATHLEN);

     std::string tmp = std::string(path1);
     std::size_t pos;
     if (tmp.find("/") == std::string::npos)
         pos = tmp.find_last_of("\\");
     else
         pos = tmp.find_last_of("/");

     pos++;
     return tmp.substr(pos, tmp.length() - pos);
}

For others that may come across this:

This can be done with boost::filesystem (http://www.boost.org/doc/libs/1_38_0/libs/filesystem)

These are the relavent functions:

boost::filesystem::initial_path();
boost::filesystem::current_path();

About initial_path():

Returns: current_path() at the time of entry to main().

[Note: These semantics turn a dangerous global variable into a safer global constant. --end note]

[Note: Full implementation requires runtime library support. Implementations which cannot provide runtime library support are encouraged to instead store the value of current_path() at the first call of initial_path(), and return this value for all subsequent calls. Programs using initial_path() are encouraged to call it immediately on entrance to main() so that they will work correctly with such partial implementations. --end note]

About current_path():

Returns: The current path, as if by POSIX getcwd().

Postcondition: current_path().is_complete()

[Note: The current path as returned by many operating systems is a dangerous global variable. It may be changed unexpectedly by a third-party or system library functions, or by another thread. Although dangerous, the function is useful in dealing with other libraries.. For a safer alternative, see initial_path(). The current_path() name was chosen to emphasize that the return is a complete path, not just a single directory name. -- end note]

You can find a reference here: http://www.boost.org/doc/libs/1_38_0/libs/filesystem/doc/reference.html

Boost: http://www.boost.org/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.