I tried to use the following code to output files in current directory
#include <filesystem>
#include <iostream>
using namespace std::filesystem;
int main() {
for (directory_iterator next("."), end; next != end; ++next) {
std::cout << next->path() << std::endl;
}
return 0;
}
the compile command is a simple g++ demo.cpp -o demo.exe, the path to g++ is C:\msys64\ucrt64\bin\g++.exe.
It worked properly when i ran it in bash (msys2),
$ ./demo.exe
".\\.clangd"
".\\.vscode"
".\\demo.cpp"
".\\demo.exe"
but when i did the same thing in powershell or cmd, it output nothing.
PS C:\Users\cnjawi> .\demo.exe
PS C:\Users\cnjawi>
The following code could run just fine in powershell and cmd.
#include <filesystem>
#include <iostream>
using namespace std::filesystem;
int main() {
std::cout << "test\n";
return 0;
}
However, if i add the original code, the issue occurs, even the "test" couldn't be output.
#include <filesystem>
#include <iostream>
using namespace std::filesystem;
int main() {
std::cout << "test\n";
for (directory_iterator next("."), end; next != end; ++next) {
std::cout << next->path() << std::endl;
}
return 0;
}
Thanks to HolyBlackCat's comment. When i double-clicked this program, Windows prompted The procedure entry point ~ could not be located, which gave the key info -- the program doesn't actually work in powershell due to some missing DLLs, rather than working but not outputting.
However, a collapsed program does not issue warnings in powershell, which made it appear to have worked "normally".
The simplest way to solve it is using the static library. For this instance, use g++ -c demo.cpp to generate demo.o and then g++ -static demo.o /ucrt64/lib/libstdc++fs.a -o demo.exe(in bash).
Thats what I see:
igorz#DESKTOP-QKLDJRN MINGW64 /c/Websites/cpp-series
$ cd "c:\Websites\cpp-series" && g++ gigel.cpp -o gigel && "c:\Websites\cpp-series"gigel
bash: cd: c:\Websites\cpp-series" && g++ gigel.cpp -o gigel && c:Websitescpp-series"gigel: No such file or directory
Thats the code(but the problem is'nt in code I think)
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
How can I change my current working directory in C++ in a platform-agnostic way?
I found the direct.h header file, which is Windows compatible, and the unistd.h, which is UNIX/POSIX compatible.
The chdir function works on both POSIX (manpage) and Windows (called _chdir there but an alias chdir exists).
Both implementations return zero on success and -1 on error. As you can see in the manpage, more distinguished errno values are possible in the POSIX variant, but that shouldn't really make a difference for most use cases.
Now, with C++17 is possible to use std::filesystem::current_path:
#include <filesystem>
int main() {
auto path = std::filesystem::current_path(); //getting path
std::filesystem::current_path(path); //setting path
}
For C++, boost::filesystem::current_path (setter and getter prototypes).
A file system library based on Boost.Filesystem will be added to the standard.
This cross-platform sample code for changing the working directory using POSIX chdir and MS _chdir as recommend in this answer. Likewise for determining the current working directory, the analogous getcwd and _getcwd are used.
These platform differences are hidden behind the macros cd and cwd.
As per the documentation, chdir's signature is int chdir(const char *path) where path is absolute or relative. chdir will return 0 on success. getcwd is slightly more complicated because it needs (in one variant) a buffer to store the fetched path in as seen in char *getcwd(char *buf, size_t size). It returns NULL on failure and a pointer to the same passed buffer on success. The code sample makes use of this returned char pointer directly.
The sample is based on #MarcD's but corrects a memory leak. Additionally, I strove for concision, no dependencies, and only basic failure/error checking as well as ensuring it works on multiple (common) platforms.
I tested it on OSX 10.11.6, Centos7, and Win10. For OSX & Centos, I used g++ changedir.cpp -o changedir to build and ran as ./changedir <path>.
On Win10, I built with cl.exe changedir.cpp /EHsc /nologo.
MVP solution
$ cat changedir.cpp
#ifdef _WIN32
#include <direct.h>
// MSDN recommends against using getcwd & chdir names
#define cwd _getcwd
#define cd _chdir
#else
#include "unistd.h"
#define cwd getcwd
#define cd chdir
#endif
#include <iostream>
char buf[4096]; // never know how much is needed
int main(int argc , char** argv) {
if (argc > 1) {
std::cout << "CWD: " << cwd(buf, sizeof buf) << std::endl;
// Change working directory and test for success
if (0 == cd(argv[1])) {
std::cout << "CWD changed to: " << cwd(buf, sizeof buf) << std::endl;
}
} else {
std::cout << "No directory provided" << std::endl;
}
return 0;
}
OSX Listing:
$ g++ changedir.c -o changedir
$ ./changedir testing
CWD: /Users/Phil
CWD changed to: /Users/Phil/testing
Centos Listing:
$ g++ changedir.c -o changedir
$ ./changedir
No directory provided
$ ./changedir does_not_exist
CWD: /home/phil
$ ./changedir Music
CWD: /home/phil
CWD changed to: /home/phil/Music
$ ./changedir /
CWD: /home/phil
CWD changed to: /
Win10 Listing
cl.exe changedir.cpp /EHsc /nologo
changedir.cpp
c:\Users\Phil> changedir.exe test
CWD: c:\Users\Phil
CWD changed to: c:\Users\Phil\test
Note: OSX uses clang and Centos gnu gcc behind g++.
Does chdir() do what you want? It works under both POSIX and Windows.
You want chdir(2). If you are trying to have your program change the working directory of your shell - you can't. There are plenty of answers on SO already addressing that problem.
Did you mean C or C++? They are completely different languages.
In C, the standard that defines the language doesn't cover directories. Many platforms that support directories have a chdir function that takes a char* or const char* argument, but even where it exists the header where it's declared is not standard. There may also be subtleties as to what the argument means (e.g. Windows has per-drive directories).
In C++, googling leads to chdir and _chdir, and suggests that Boost doesn't have an interface to chdir. But I won't comment any further since I don't know C++.
Nice cross-platform way to change current directory in C++ was suggested long time ago by #pepper_chico. This solution uses boost::filesystem::current_path().
To get the current working directory use:
namespace fs = boost::filesystem;
fs::path cur_working_dir(fs::current_path());
To set the current working directory use:
namespace fs = boost::filesystem;
fs::current_path(fs::system_complete( fs::path( "new_working_directory_path" ) ));
Bellow is the self-contained helper functions:
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include <string>
namespace fs = boost::filesystem;
fs::path get_cwd_pth()
{
return fs::current_path();
}
std::string get_cwd()
{
return get_cwd_pth().c_str();
}
void set_cwd(const fs::path& new_wd)
{
fs::current_path(fs::system_complete( new_wd));
}
void set_cwd(const std::string& new_wd)
{
set_cwd( fs::path( new_wd));
}
Here is my complete code-example on how to set/get current working directory:
#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/path.hpp"
#include <iostream>
namespace fs = boost::filesystem;
int main( int argc, char* argv[] )
{
fs::path full_path;
if ( argc > 1 )
{
full_path = fs::system_complete( fs::path( argv[1] ) );
}
else
{
std::cout << "Usage: tcd [path]" << std::endl;
}
if ( !fs::exists( full_path ) )
{
std::cout << "Not found: " << full_path.c_str() << std::endl;
return 1;
}
if ( !fs::is_directory( full_path ))
{
std::cout << "Provided path is not a directory: " << full_path.c_str() << std::endl;
return 1;
}
std::cout << "Old current working directory: " << boost::filesystem::current_path().c_str() << std::endl;
fs::current_path(full_path);
std::cout << "New current working directory: " << boost::filesystem::current_path().c_str() << std::endl;
return 0;
}
If boost installed on your system you can use the following command to compile this sample:
g++ -o tcd app.cpp -lboost_filesystem -lboost_system
Can't believe no one has claimed the bounty on this one yet!!!
Here is a cross platform implementation that gets and changes the current working directory using C++. All it takes is a little macro magic, to read the value of argv[0], and to define a few small functions.
Here is the code to change directories to the location of the executable file that is running currently. It can easily be adapted to change the current working directory to any directory you want.
Code :
#ifdef _WIN32
#include "direct.h"
#define PATH_SEP '\\'
#define GETCWD _getcwd
#define CHDIR _chdir
#else
#include "unistd.h"
#define PATH_SEP '/'
#define GETCWD getcwd
#define CHDIR chdir
#endif
#include <cstring>
#include <string>
#include <iostream>
using std::cout;
using std::endl;
using std::string;
string GetExecutableDirectory(const char* argv0) {
string path = argv0;
int path_directory_index = path.find_last_of(PATH_SEP);
return path.substr(0 , path_directory_index + 1);
}
bool ChangeDirectory(const char* dir) {return CHDIR(dir) == 0;}
string GetCurrentWorkingDirectory() {
const int BUFSIZE = 4096;
char buf[BUFSIZE];
memset(buf , 0 , BUFSIZE);
GETCWD(buf , BUFSIZE - 1);
return buf;
}
int main(int argc , char** argv) {
cout << endl << "Current working directory was : " << GetCurrentWorkingDirectory() << endl;
cout << "Changing directory..." << endl;
string exedir = GetExecutableDirectory(argv[0]);
ChangeDirectory(exedir.c_str());
cout << "Current working directory is now : " << GetCurrentWorkingDirectory() << endl;
return 0;
}
Output :
c:\Windows>c:\ctwoplus\progcode\test\CWD\cwd.exe
Current working directory was : c:\Windows
Changing directory...
Current working directory is now : c:\ctwoplus\progcode\test\CWD
c:\Windows>
I have a big trouble while writing some data to files using MPI on a cluster with PBS. Here is the example of simple problem-emulating programm.
#include <mpi.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <unistd.h>
int main(int argc, char* argv[]){
int rank;
int size;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Define hostname
char hostname[128];
gethostname(hostname, 128);
// check and create dump directory
struct stat buf;
int rc;
char *dir="Res";
rc = stat( dir, &buf );
if( rc ) // no dir, create
{ if( rank == 0 )
{
rc = mkdir( dir, 0771);
if( rc )
{std::ostringstream oss;
oss << "Can't create dump directory \""
<< dir
<< "\"";
}
}
else {
sleep (2);
}
}
else if( !S_ISDIR( buf.st_mode ) )
{std::ostringstream oss;
oss << "Path \""
<< dir
<< "\" is not directory for dump";
}
MPI_Barrier(MPI_COMM_WORLD);
// Every process defines name of file for output (res_0, res_1, res_2.....)
std::ostringstream filename;
filename << dir << "/res_"<< rank;
// Open file
std::ofstream file(filename.str().c_str());
// Output to file . Output seems like "I am 0 from 24. hostname"
file << "I am " << rank << " from " << size << ". " << hostname << std::endl;
file.close();
MPI_Finalize();
return 0;
}
I compile it with openmpi_intel-1.4.2, using comand
mpicxx -Wall test.cc -o test
Then I queue this program with script:
#!/bin/bash
#PBS -N test
#PBS -l select=8:ncpus=6:mpiprocs=6
#PBS -l walltime=00:01:30
#PBS -m n
#PBS -e stderr.txt
#PBS -o stdout.txt
cd $PBS_O_WORKDIR
echo "I run on node: `uname -n`"
echo "My working directory is: $PBS_O_WORKDIR"
echo "Assigned to me nodes are:"
cat $PBS_NODEFILE
mpirun -hostfile $PBS_NODEFILE ./test
I expected this result:
1. New directory "Res" to be created
2. 8*6 different files (res_0, res_1, res_2, ...) to be written to the Res dir
But only res_* file from the first node are written (res_{0..5}) while the rest are not.
What is the problem?
Thank you!
OK, let's assume you run on a file system coherently mounted across all your compute nodes. This is the case, right?
So then the main issue I see with your code snippet is that all processes do state the directory at the same time and then try to create it if it doesn't exist. I'm not sure what truly happens but I'm sure this isn't the smartest idea ever.
Since in essence what you want is a serial sanity check of the directory and/or it's creation if needed, why not just letting MPI process of rank 0 doing it?
That would give you something like this:
if ( rank == 0 ) { // Only master manages the directory creation
int rc = stat( dir, &buf );
... // sanity check goes here and directory creation as well
// calling MPI_Abort() in case of failure seems also a good idea
}
// all other processes wait here
MPI_Barrier( MPI_COMM_WORLD );
// now we know the directory exists and is accessible
// let's do our stuff
Could this work for you?
I am trying to find out if a command line program is installed, so that it can be used later.
So far what I have tried is:
int whichReturn = system("command -v THE_CL_PROGRAM >/dev/null && { exit 50; }|| { exit 60; }");
if (whichReturn == 12800) { //system 'apparently' returns the return value *256 (50*256 = 12800)
//...
}
However it seems that it always returns 60 and so fails.
Is there an easier way to do this? Or can someone point out where my mistake is please?
Thanks
A complete program using which:
isthere.cpp:
#include <iostream>
#include <cstdlib>
#include <sstream>
int main(int argc, char* argv[])
{
std::ostringstream cmd;
cmd << "which " << argv[1] << " >/dev/null 2>&1";
bool isInstalled = (system(cmd.str().c_str()) == 0);
std::cout << argv[1] << " is "<< ((isInstalled)?"":"NOT ") << "installed! << std::endl;
}
Output:
$ ./isthere ls
ls is installed!
$ ./isthere grep
grep is installed!
$ ./isthere foo
foo is NOT installed!