函数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>定义函数: ...
随机推荐
- Working with Python subprocess - Shells, Processes, Streams, Pipes, Redirects
Posted: 2009-04-28 15:20 Tags: Python Note Much of the "What Happens When you Execute a Command ...
- Python系列之反射、面向对象
一.反射 说反射之前先介绍一下__import__方法,这个和import导入模块的另一种方式 1. import commons 2. __import__('commons') 如果是多层导入: ...
- ZOJ2067 经典 DP
题目:一个由'.'和'#'组成矩形,统计里面'.'组成的矩形的个数.点击打开链接 自己写挂了,懒得搞了 #include <stdio.h> #include <string.h&g ...
- 玩转INotifyPropertyChanged和ObservableCollection
转载: http://www.cnblogs.com/Jax/archive/2009/10/13/1582128.html 本文的代码都是基于WPF的,对于Silverlight,这些技术也同样 ...
- Jmeter脚本录制方法(二)——手工编写脚本(jmeter与fiddler结合使用)
jmeter脚本录制方法可以分三种,前几天写的一篇文章中,已介绍了前两种,今天来说下第三种,手工编写脚本,建议使用这一种方法,虽然写的过程有点繁琐,但调试脚本比前两者方式都要便捷. 首先来看下三种方式 ...
- 使用Canvas制作时钟动画
复习Javascript到Canvas的知识点,看到一个使用Canvas绘制的静态时钟例子,便想将其变成动态显示系统时间的时钟动画.另外再配上数字显示的时钟,一个小的时钟模块的诞生了!目前的界面还比较 ...
- OpenGL ES2.0贴图
1.定义传入着色器的顶点数据及索引 //传入结构体 typedef struct { ]; ]; } Vertex; //顶点数据 const Vertex Vertices[] = { {{, -, ...
- 压缩感知“Hello World”代码初步学习
压缩感知代码初学 实现:1-D信号压缩传感的实现 算法:正交匹配追踪法OMP(Orthogonal Matching Pursuit) >几个初学问题 1. 原始信号f是什么?我采集的是 ...
- 【转】Sizeof与Strlen的区别与联系
原文地址:http://www.cnblogs.com/carekee/articles/1630789.html 1.sizeof sizeof(...)是运算符,在头文件中typedef为uns ...
- 【转】深度分析NandFlash—物理结构及地址传送(以TQ2440开发板上的K9F2G08U0A为例)
K9F2G08U0A是三星公司生产的总容量为256M的NandFlash,常用于手持设备等消费电子产品.还是那句话,搞底层就得会看datasheet,我们就从它的datasheet看起. 这就是 K9 ...