Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C++ equivalent to getcwd?

Tags:

c++

getcwd

I see C's getcwd via: man 3 cwd

I suspect C++ has a similar one, that could return me a std::string .

If so, what is it called, and where can I find it's documentation?

Thanks!

like image 219
anon Avatar asked Feb 04 '10 20:02

anon


People also ask

What is Getcwd in C?

The getcwd() function shall place an absolute pathname of the current working directory in the array pointed to by buf, and return buf. The pathname copied to the array shall contain no components that are symbolic links. The size argument is the size in bytes of the character array pointed to by the buf argument.

How do I get the current working directory in Perl?

cwd() gets the current working directory using the most natural and safest form for the current architecture. For most systems it is identical to `pwd` (but without the trailing line terminator). getcwd() does the same thing by re-implementing getcwd(3) or getwd(3) in Perl.

Is Getcwd thread safe?

Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library.


1 Answers

Ok, I'm answering even though you already have accepted an answer.

An even better way than to wrap the getcwd call would be to use boost::filesystem, where you get a path object from the current_path() function. The Boost filesystem library allows you to do lots of other useful stuff that you would otherwise need to do a lot of string parsing to do, like checking if files/directories exist, get parent path, make paths complete etcetera. Check it out, it is portable as well - which a lot of the string parsing code one would otherwise use likely won't be.

Update (2016): Filesystem has been published as a technical specification in 2015, based on Boost Filesystem v3. This means that it may be available with your compiler already (for instance Visual Studio 2015). To me it also seems likely that it will become part of a future C++ standard (I would assume C++17, but I am not aware of the current status).

Update (2017): The filesystem library has been merged with ISO C++ in C++17, for

std::filesystem::current_path(); 
like image 135
villintehaspam Avatar answered Sep 28 '22 07:09

villintehaspam