函数chdir、fchdir和getcwd
函数chdir、fchdir和getcwd
chdir、fchdir函数
#include<unistd.h>int chdir(constchar*pathname);int fchdir(int fd);Bothreturn:0if OK,−1 on error
实例
/*** 文件内容:因为当前工作目录是进程的一个属性,所以它只影响到调用chdir的进程本身* 而不影响其他进程* 文件时间:* 作者:firewaywei@126.com*/#include<stdio.h>#include<stdlib.h>int main(void){if(chdir("/tmp")<0){err_sys("chdir failed");}printf("chdir to /tmp succeeded\n");exit(0);}
gcc main.c -lerror -Llib
$ pwd
/home/fireway/study/temp2
$ ./a.out
chdir to /tmp succeeded
getcwd函数
#include<unistd.h>char*getcwd(char*buf,size_t size);Returns: buf if OK, NULL on error
实例
/*** 文件名:mycwd.c* 文件内容: 将工作目录更改至一个指定目录,然后调用getcwd,最后打印该工作目录* 时间:2016年 11月 14日 星期一 07:59:08 CST* 作者:firewaywei@126.com*/#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include"pathalloc.h"int main(void){char*ptr = NULL;size_t size =0;if(chdir("/usr/spool/uucppublic")<0){err_sys("chdir failed");}ptr = path_alloc(&size);if(getcwd(ptr, size)== NULL){err_sys("getcwd failed");}printf("cwd = %s\n", ptr);if(ptr != NULL){free(ptr);ptr = NULL;}exit(0);}
gcc main.c -lerror -L../temp3
# ln -s /home/fireway/study/temp3 /usr/spool
# ./a.out
cwd = /home/fireway/study/temp3/uucppublic
# ls -l /usr/spool
lrwxrwxrwx 1 root root 25 11月 14 08:24 /usr/spool -> /home/fireway/study/temp3
参考
函数chdir、fchdir和getcwd的更多相关文章
- 文件和目录之chdir、fchdir和getcwd函数
每个进程都有一个当前工作目录,此目录是搜索所有相对路径名的起点(不以斜杠开始的路径名为相对路径名).当用户登录到UNIX系统时,其当前工作目录通常是口令文件(/etc/passwd)中该用户登录项的第 ...
- apue chapter 4 文件和目录
1.文件信息结构体 struct stat{ mode_t st_mode; //file type and permissions ino_t st_ino; //i-node number (se ...
- UNIX环境高级编程--4
函数stat fstat fstatat 和 lstat stat函数使用最多的地方可能就是ls -l 命令,用其可以获得有关一个文件的所有信息. 文件类型: (1)普通文件 (2)目录文件 (3)块 ...
- Files and Directories
Files and Directories Introduction In the previous chapter we coveredthe basic functions that pe ...
- 【APUE】Chapter4 File and Directories
4.1 Introduction unix的文件.目录都被当成文件来看待(vi也可以编辑目录):我猜这样把一起内容都当成文件的原因是便于统一管理权限这类的内容 4.2 stat, fstat, fst ...
- 文件权限控制篇access alphasort chdir chmod chown chroot closedir fchdir fchmod fchown fstat ftruncate getcwd
access(判断是否具有存取文件的权限) 相关函数 stat,open,chmod,chown,setuid,setgid 表头文件 #include<unistd.h> 定义函数 in ...
- getcwd函数学习
getcwd 函数原型:char *getcwd( char *buffer, int maxlen ); 功 能:获取当前工作目录 参数说明:getcwd()会将当前工作目录的绝对路径复制到参数bu ...
- Linux中文件函数(二)
一.link.linkat.unlink.unlinkat.remove函数 创建一个指向现有文件的链接的方法是使用link函数或linkat函数.函数的原型为: #include <unist ...
- 【Linux C中文函数手册】之 目录操作函数
目录操作函数 1)closedir 关闭目录 相关函数: opendir表头文件: #include<sys/types.h> #include<dirent.h>定义函数: ...
随机推荐
- Sqlite常用sql语句
sqlite常用sql语句 --返回UTC时间 select CURRENT_TIMESTAMP; --返回本地时间 select datetime(CURRENT_TIMESTAMP,'localt ...
- 记录一次因为硬盘写满造成的redis无法连接
缘起: 今天早晨收到报警,服务不干活了,赶紧起来看问题... 为了尽快让服务可用,尝试重启服务,发现服务起不来,报错 redis connection failed! 看起来是redis挂了,但是发现 ...
- live555_RTSP连接建立以及请求消息处理过程
1,RTSP连接的建立过程 RTSPServer类用于构建一个RTSP服务器,该类同时在其内部定义了一个RTSPClientSession类,用于处理单独的客户会话. 首先创建RTSP服务 ...
- MongoDB快速入门
http://www.yiibai.com/mongodb/mongodb_quick_guide.html 创建数据库 MongoDB use DATABASE_NAME 用于创建数据库.该命令如果 ...
- 03Vue事件
Vue提供了事件的绑定,方法写在methods对象中. 绑定dom中有两种方法: 方法一:v-on:click/dblclcick/mouseOver/mouseOut="方法名" ...
- NodeJs之数据库异常处理
数据库异常 NodeJs版本:4.4.4 数据库链接错误 使用nodejs处理异常最麻烦不过,这里我抛开nodejs提供的domain和一些第三方库专门处理的东西.操作数据库是我们常用的功能.通过回调 ...
- [Python] 文科生零基础学编程系列一——对象、集合、属性、方法的基本定义
1.编程语言: 1.1是什么: 编程语言(programming language),是用来定义计算机程序的形式语言.它是一种被标准化的交流技巧,用来向计算机发出指令. 一种计算机语言让程序员能够准确 ...
- 网页单位和rem小分享
有哪些网页尺寸单位? CSS 中的单位有很多种: 百分比(%) 英寸(in) 厘米(cm) 毫米(mm) 磅数(pt) 12 点活字(pc) 字母高度一半(ex) 父级字体(em) 像素(px) 根元 ...
- WebService调用(基于KSOAP2)
public static boolean getData(String param) { //WebService服务器地址 String SERVICE_URL = "http://22 ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...