1. 程式人生 > >C\C++ 獲取當前路徑

C\C++ 獲取當前路徑

獲取當前工作目錄是使用函式:getcwd。cwd指的是“current working directory”,這樣就好記憶了。

函式說明:

    函式原型:char* getcwd(char* buffer, int len);

    引數:buffer是指將當前工作目錄的絕對路徑copy到buffer所指的記憶體空間, len是buffer的長度。

    返回值:獲取成功則返回當前工作目錄(絕對路徑),失敗則返回false(即NULL)。 

    該函式所屬標頭檔案為<direct.h>

具體使用如下例:(特別注意的是,當你使用的是Linux系統時,請注意你是否具有相關的許可權,如果許可權不夠會導致獲取失敗)

#include <stdio.h>
#include <direct.h>

int main()
{
	char *buffer;
	//也可以將buffer作為輸出引數
	if((buffer = getcwd(NULL, 0)) == NULL)
	{
		perror("getcwd error");
	}
	else
	{
        printf("%s\n", buffer);
		free(buffer);
	}
}
執行後可返回工作目錄的絕對路徑…………

相關推薦

c# winform獲取當前路徑exe程式的版本號

 通常軟體自動更新時,需要比較新版本與舊版本的版本號,如果新檔案的版本號比舊檔案高,就替換舊檔案。(版本號一般可以在Visual Studio的程式資訊裡定義)  如何取得當前exe程式的版本號呢

C# 獲取當前路徑方法(轉)

關聯 direct 引入 安全 判斷 是我 forms 字符串 sof C# 獲取當前路徑方法 //獲取包含清單的已加載文件的路徑或 UNC 位置。 public static string sApplicationPath = Assembly.

C#獲取當前路徑的七種方法

filename ces cut lease rect 包括 name system file //1.獲取模塊的完整路徑。 string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModul

C# 獲取當前路徑方法整理

包含 windows應用 指定 soft 對話框 lba return gre framework https://www.cnblogs.com/tianma3798/p/6553863.html1. //獲取包含清單的已加載文件的路徑或 UNC 位置。 pu

C#獲取當前路徑的幾種方法

size start orm name uri path ant tom 當前 //1.獲取模塊的完整路徑。 string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileNam

C語言獲取當前編譯執行檔案(XXX.exe)檔名和目錄路徑

C語言獲取當前編譯執行檔案(XXX.exe)檔名和目錄路徑 vc2010編譯通過 #include<stdio.h> #include<string.h> #include<windows.h> #include<stdlib.h> int ma

C#獲取當前路徑的方法如下

C#獲取當前路徑的方法如下: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -獲取模組的完整路徑。 2. System.Environment.CurrentDirectory -獲取和設

linux C語言獲取當前可執行程式的路徑及檔名 vs 獲取當前工作路徑

 獲取當前工作路徑: 標頭檔案:#include <unistd.h> 定義函式:char * getcwd(char * buf, size_t size); 函式說明:getcwd()會將當前的工作目錄絕對路徑複製到引數buf 所指的記憶體空間,引數size

C#: 獲取當前路徑不要用Environment.CurrentDirectory

網上大把文章寫到C#獲取當前路徑的方法如下: // 獲取程式的基目錄。 System.AppDomain.CurrentDomain.BaseDirectory // 獲取模組的完整路徑。 System.Diagnostics.Process.GetCurrentProcess().MainModul

C\C++ 獲取當前路徑

獲取當前工作目錄是使用函式:getcwd。cwd指的是“current working directory”,這樣就好記憶了。 函式說明:     函式原型:char* getcwd(char* buffer, int len);     引數:buffer是指將當前工作

C#獲取當前路徑的方法總結

C#獲取當前路徑的方法如下: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -獲取模組的完整路徑。 2. System.Environment.CurrentDirectory

C\C++獲取當前路徑

獲取當前工作目錄是使用函式:getcwd。cwd指的是“current working directory”,這樣就好記憶了。 函式說明:     函式原型:char* getcwd(char* buffer, int len);     引數:buffer是指將當前工作

c語言獲取當前工作路徑(轉)

------------------------------------------------------------------------------------- Linux --------------------------------------------------------------

C#獲取當前路徑的7種方法

C#獲取當前路徑的方法如下: 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName -獲取模組的完整路徑。 2. System.Environment.CurrentDirectory

C++中獲取當前執行路徑

獲取.exe執行路徑 多位元組集環境下 #include "stdafx.h" #include <Windows.h> #include <iostream> #include <string> using names

linux下c語言獲取當前時間

keyword spa pre urn markdown view 技術分享 時間 tle 和時間有關的函數定義在頭文件”time.h”中 常用函數: time_t time(time_t *t); 函數說明:此函數會返回從公元 1970 年1 月1 日的UTC

C C++如何獲取當前系統時間

C庫中與系統時間相關的函式定義在<time.h>標頭檔案中, C++定義在<ctime>標頭檔案中。 一、time(time_t*)函式 函式定義如下: time_t time (time_t* timer);

C語言獲取當前時間(Linux環境下,VC6.0,Codeblock環境下通用)

在Linux環境下用C語言編寫程式獲取當前的時間只要呼叫其內部的函式即可。這些函式在 time.h 這個標頭檔案裡,第一個函式原型: ① time_t time(time_t *t),通過Linux的man也很方便能夠找到這個函式的相關說明: 在Linux環境的命令列模式中輸入 man 2 time即

c# 關於獲取exe路徑

C#獲取程式路徑的方法中需要注意的地方(版本1) 獲取執行程式路徑的方法主要有以下7種: System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName 他獲取的是Module的檔名,如果在VS200

C++中獲取當前時間並格式化輸出

1 #include <string> 2 #include <time.h> 3 using namespace std; 4 5 string getTime() 6 { 7 time_t timep; 8 time (&timep);