函数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>定义函数: ...
随机推荐
- python之字典
1.用{}创建字典 代码: 1 2 x = {"a":"1", "b":"2"} print x 输出: {'a': ' ...
- Java 继承、抽象、接口
一.继承 1. 概述 继承是面向对象的重要特征之一,当多个类中存在相同的属性和行为时,将这些内容抽取到单独一个类中,那多个类中无需再定义这些属性和行为,只需继承那个单独的类即可. 单独的类称为父类或超 ...
- 接口自动化测试方案PHP + mysql
接口测试在测试工作中是很常见的工作,但是在以往的接口测试工作中借助的一般是第三方插件.python开发的发送请求脚本.LR脚本.Jmeter脚本,之前也使用python开发了一套接口自动化测试系统,但 ...
- win10 + python3.6 + VSCode + tensorflow-gpu + keras + cuda8 + cuDN6N环境配置
写在前面的话: 再弄这个之前,我对python也好,tensorflow也好几乎是0认知的,所以配置这个环境的时候,走了不少弯路,整整耗费了一个星期的时间才搭配完整这个环境,简直了...然而最气的是, ...
- gcd,最大公约数,lcm,最小公倍数
int gcd(int a,int b){ ?a:gcd(b,a%b); } 关于lcm,若写成a*b/gcd(a,b) ,a*b可能会溢出! int lcm(int a,int b){ return ...
- C++点滴20130724
warning c4627:"#include stdafx.h":在查找预编译头使用时跳过 原因: 1.没有添加 #include "stdafx.h" 2. ...
- 常用Linux操作命令
查看物理CPU个数:cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l 查看每个物理CPU中的核数:cat /proc ...
- Hadoop(十三)分析MapReduce程序
前言 刚才发生了悲伤的一幕,本来这篇博客马上就要写好的,花了我一晚上的时间.但是刚才电脑没有插电源就没有了.很难受!想哭,但是没有办法继续站起来. 前面的一篇博文中介绍了什么是MapReduce,这一 ...
- 字符串解码DecodeString
字符串解码 原创博客,转载请注明出处!eg:ss=2[abc]3[cd]ef return:abcabccdcdcdefss=3[a2[c]]2[a] return:accaccaccaas ...
- Java基础(00)
Java发展史 Java之父:詹姆斯.高斯林(James Gosling). SUN(Stanford University Network 斯坦福大学网络公司)产物. 1995年5月23日,java ...