
Algumas tarefas básicas muitas vezes servem para memorizarmos conceitos fundamentais.
Some basic tasks often serve to memorize fundamental concepts.
So, in this article today we will see how to get the current directory in 10 different programming languages. Among them: C++, C, Lua, Java, Bash, JavaScript, Python, PHP, Golang e Ruby.
All were used as simply as possible and in a modern way. It will also serve to review how to concatenate and basic syntax.
In each code there will also be a comment according to the language to compile and/or interpret via the command line.
#include <iostream>
#include <filesystem>
int main(){
std::cout << "The current directory is: "
<< std::filesystem::current_path() << '\n';
return 0;
}
// g++ -std=c++17 current.cpp && ./a.out#include <stdio.h>
#include <unistd.h>
#include <limits.h>
int main() {
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
printf("The current directory is: %s\n", cwd);
return 0;
}
// gcc current.c && ./a.outprint("The current directory is: " .. os.getenv("PWD"))
--lua current.luapublic class current {
public static void main(String[] args) {
System.out.println("The current directory is: " + System.getProperty("user.dir"));
}
}
// javac current.java && java current echo "The current directory is: ${PWD}"
# bash current.shconsole.log("The current directory is: " + process.cwd() )
// node current.jsimport os
print("The current directory is: " + os.getcwd())
# python current.py<?php echo "The current directory is: " . getcwd() . "\n";
# php current.phppackage main
import (
"fmt"
"os"
)
func main(){
dir,err := os.Getwd() ; _ = err
fmt.Println("The current directory is: " + dir)
}
// go build current.go && ./current puts "The current directory is: " + Dir.pwd
# ruby current.rbcpp clanguage lua bash javascript python php go ruby