Is there a C++ equivalent to getcwd?
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();
How to get the current directory in a C program?
Have you had a look at getcwd()
?
#include <unistd.h>
char *getcwd(char *buf, size_t size);
Simple example:
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
int main() {
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working dir: %s\n", cwd);
} else {
perror("getcwd() error");
return 1;
}
return 0;
}
C programming getcwd variable manipulation
What about the following code it's work for me on ubuntu
-
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
int main( void ){
char* cwd;
char buff[PATH_MAX + 1];
cwd = getcwd( buff, PATH_MAX + 1 );
if( cwd != NULL ) {
printf( "My working directory is %s.\n", cwd );
if(strcmp("/home/razib/Desktop", cwd) == 0) {
printf("I'm in Desktop now\n");
}
}
return EXIT_SUCCESS;
}
Here you have to provide getcwd()
method a buff[]
. The buff[]
may be declared with size PATH_MAX+1
. PATH_MAX
can be found at limits.h
.
Hope it will help you.
Thanks a lot.
What is a cross-platform way to get the current directory?
If it is no problem for you to include, use boost filesystem for convenient cross-platform filesystem operations.
boost::filesystem::path full_path( boost::filesystem::current_path() );
Here is an example.
EDIT: as pointed out by Roi Danton in the comments, filesystem became part of the ISO C++ in C++17, so boost is not needed anymore:
std::filesystem::current_path();
C, Linux, getcwd/chdir(): get binary path
This is exactly the kind of thing autoconf lives for, and supporting those standard directories is pretty much mandatory if you ever want anyone other than the programmers who wrote your software to use it. Once set up properly, to debug out of your home directory all you have to do is pass a different --prefix=
value to configure
.
Wrap C block in function. Beginner question
Theres a number of things that you are doing wrong here:
void getCurrentDirectory(char *directory)
{
getcwd(directory, sizeof(directory));
}
Mistake 1:
`sizeof(directory)`
gives you size of a pointer, to be precise, char *. Your intention is to pass size of the array, and not the pointer size.
Mistake 2:
`printf("%s", *directory);`
Passes first element of the array to the printf not the address of the array. Your intention is to print the entire array not just the first element.
Corrected Solution
You should be doing
void getCurrentDirectory(char *directory, size_t arrSize)
{ ^^^^^^^^^^^^^^
getcwd(directory, arrSize);
}
Size of the array is passed explicitly so the function can just use it.
In the main while printing the contents of the array:
printf("%s", directory);
How to get the path to the current file (pwd) in Linux from C?
Simply opening and reading directories does not change the current working directory. However, changing directory in your program will.
for reference,
#include <unistd.h>
#include <stdio.h>
int main() {
char cwd[1024];
chdir("/path/to/change/directory/to");
getcwd(cwd, sizeof(cwd));
printf("Current working dir: %s\n", cwd);
}
Related Topics
How to Benchmark Boost Spirit Parser
Three Forward Slashes for Block Commenting
How to Assign a Value to the Pointer 'This' in C++
Dangling References and Undefined Behavior
Std::Optional - Construct Empty with {} or Std::Nullopt
Why Is Taking the Address of a Temporary Illegal
How to Open a Folder in %Appdata% with C++
Is There a Gcc Preprocessor Directive to Check If the Code Is Being Compiled on a 64 Bit MAChine
Calling Constructor with Braces
What Standard Clause Mandates This Lvalue-To-Rvalue Conversion
Create Shared_Ptr to Stack Object
Is It Legal to Modify the Result of Std::String::Op[]
How Is Floating Point Conversion Actually Done in C++(Double to Float or Float to Double)