This language bar is your friend. Select your favorite languages!

Idiom #106 Get program working directory

Assign to string dir the path of the working directory.
(This is not necessarily the folder containing the executable itself)

use Cwd;
my $dir = getcwd();
with Ada.Directories; use Ada.Directories;
Dir : String := Current_Directory;
#include <unistd.h>
char *dir = getcwd(NULL, 0);
#include <filesystem>
dir = std::filesystem::current_path();
using System.IO;
string path = Directory.GetCurrentDirectory();
import std.path;
string dir = absolutePath;
use iso_c_binding, only: c_char, c_size_t,c_ptr, c_null_ptr, c_associated
     function c_getcwd (buf, size) bind(C,name="getcwd") result(r)
       type(c_ptr) :: r
       character(kind=c_char), dimension(*), intent(out) :: buf
       integer(kind=c_size_t), value :: size
     end function c_getcwd
  end interface

    if (c_associated(c_getcwd (buf, size(buf,kind=c_size_t)))) then
       n = findloc(buf,achar(0),1)
       allocate (character(len=n-1) :: dir)
       dir(1:n-1) = transfer(buf(1:n-1),dir(1:n-1))
    end if
import "os"
dir, err := os.Getwd()
def dir = new File('.').absolutePath
import System.Directory
dir <- getCurrentDirectory
let dir = process.cwd ()
String path = this.getClass().getClassLoader().getResource("").getPath();
import java.io.File;
String dir = new File("").getAbsolutePath();
String dir = System.getProperty("user.dir");
dir = os.getenv("PWD") or io.popen("cd"):read()
$dir = getcwd();
dir := expandfilename('.');
import os
dir = os.getcwd()
dir = Dir.pwd
use std::env;
let dir = env::current_dir().unwrap();

New implementation...
< >