linux C 获取当前的工作目录

#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
	char buffer[1024] ;

	//获取当前的工作目录,注意:长度必须大于工作目录的长度加一
	char *p = getcwd(buffer , 40);
	char *dir = NULL;

	printf("buffer:%s   p:%s size:%d  \n" , buffer , p , strlen(buffer));
	//获取当前工作目录的名字
	dir = (char *)get_current_dir_name();
	printf("dir:%s \n" , dir);

	char *twd = NULL ; 

	twd = getwd(buffer);

	printf("buffer:%s   twd:%s \n" , buffer , twd);

	return 0 ;
}

运行结果:

buffer:   p:(null) size:0  
dir:/mnt/sdb1/yangyx/system系统编程/文件目录接口及声卡实现 
buffer:/mnt/sdb1/yangyx/system系统编程/文件目录接口及声卡实现   twd:/mnt/sdb1/yangyx/system系统编程/文件目录接口及声卡实现

时间: 2024-07-31 19:37:19

linux C 获取当前的工作目录的相关文章

【Python笔记】【os】-- 获取设置当前工作目录

获取当前工作目录 os.getcwd() #用以获取当前的工作目录 改变当前工作目录 os.chdir(path) #用于改变当前工作目录到指定的路径 参数: Path  --要切换到的路径. 返回值:如果允许访问返回True,否则返回False. 例子: #-*-coding:utf-8-*- import os,sys path = 'D:\\' #查看当前工作目录 print("当前的工作目录为:%s" %os.getcwd()) #修改当前工作目录 os.chdir(path)

linux下的cd 切换工作目录

linux下cd命令 功能:切换当前工作目录 使用格式:cd [目录名] (cd和目录之间使用空格隔开) 常用命令 1.进入用户主目录 cd cd $home cd ~ 上面三个命名都能实现进入用户主目录的任务 2.返回进入此目录之前所在的目录 cd - 3.返回上级目录 cd .. 若当前目录为"/",则执行完后还在"/": ".."为上级目录的意思 4.返回上两级目录: cd ../.. 5.转到系统根目录 cd / 例如:cd /home/

.NET 获取项目的工作目录和执行目录

1.获取和设置当前工作目录 a)          System.IO.Directory.GetCurrentDirectory() 说明:获取应用程序的当前工作目录的完全限定路径.该方法是对Win32 API的GetCurrentDirectory函数的一个封装. 例如:'"C:\Documents and Settings\Administrator\My Documents" b)         System.Environment.CurrentDirectory 说明:获

Linux 下pwd 查看当前工作目录命令

pwd 命令 含义:Print Working Directory 功能:打印当前的完整工作目录.

Linux Shell编程之softlink invoke与工作目录问题

softlink 又叫做symbolic link,相当于一种"快捷方式",由ln –s sourcefile distinctionfile (ln –s TARGET LINK_NAME)创建,连接文件的存在使得简化了文件系统,用户操作起来更加方便.但在Shell编程中却是一个需要非常注意的点,因为工作目录(Work Directory)的原因,Shell需要使用"标准"的变量(跟系统环境变量有些类似)来定位它当前的工作目录以及这些目录中的文件,假如工作目录定义

设置当前exe执行文件为进程工作目录

设置当前exe执行文件为进程工作目录 两种办法: 1,   API void _splitpath( const char *path, char *drive, char *dir, char *fname, char *ext ); 这个函数将文件全名(带路径)分解成路径名,文件名,后缀名. 2, API BOOL PathRemoveFileSpec(           LPTSTR pszPath ); 使用例子: #include <windows.h> #include <

Windows和Linux下获取当前可执行文件路径和工作目录

1.Windows下的获取当前可执行文件的路径和当前工程目录. (1)获取当前可执行文件路径: #include <shlwapi.h> #pragma comment(lib, "shlwapi.lib") wchar_t szExePath[MAX_PATH] = {0}; GetModuleFileNameW(NULL, szExePath, sizeof(szExePath)); PathRemoveFileSpecW(szExePath); (2)如果想获取当前工

linux下apache更换自己的工作目录

修改apache2的默认文档目录(默认是在/var/www) 修改命令:sudo gedit /etc/apache2/sites-enabled/000-default 在文档中找到 DocumentRoot 在后面修改你要放置网页文件的目录 修改完后重启apache2服务器即可,重启命令: sudo /etc/init.d/apache2 restart 在linux下开发html.php等程序时,默认要到/var/www目录下才能工作,而/var/www目录必须要有超级用户权限才能访问,还

获取当前目录getcwd,设置工作目录chdir,获取目录信息

#include <unistd.h> #include <stdio.h> #include <limits.h> int main(int argc, char* argv[]) { char buf[PATH_MAX]; getcwd(buf, PATH_MAX-1); printf("the current path is :%s\n", buf); return 0; } 设置工作目录: #include <unistd.h>