#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv){
pid_t pid;
pid =fork();
if (pid< 0){
fprintf(stderr,"Fork failed! \n");
}else if(0==pid){
execlp("pwd",NULL);
}else{
int pid_son= wait(NULL);
int pid_fat= getpid();
printf("%d %d\n",pid_son,pid_fat);
/*
int access(const char* pathname,int mode)
mode: the mode of operation
成功执行时,返回0。失败返回-1,errno被设为以下的某个值
EINVAL: 模式值无效
EACCES: 文件或路径名中包含的目录不可访问
ELOOP : 解释路径名过程中存在太多的符号连接
ENAMETOOLONG:路径名太长
ENOENT:路径名中的目录不存在或是无效的符号连接
ENOTDIR: 路径名中当作目录的组件并非目录
EROFS: 文件系统只读
EFAULT: 路径名指向可访问的空间外
EIO:输入输出错误
ENOMEM: 不能获取足够的内核内存
ETXTBSY:对程序写入出错
mode:
R_OK 测试读许可权
W_OK 测试写许可权
X_OK 测试执行许可权
F_OK 测试文件是否存在
*/
if(access("./hise",F_OK)==-1){
/*
return 0 or -1
set error
*/
int val=mkdir("./hise",0777);
if (!val){
printf("dir created success\n");
char * file="proc1.c";
char * des_path="./hise/new.c";
int fd1=open(file,O_RDWR|O_CREAT);
int fd2=open(des_path,O_RDWR|O_CREAT);
int len;
char buff[1024];
printf("----");
while(len=read(fd1,buff,1024))
write(fd2,buff,len);
}
else
fprintf(stderr,"failed to create dir\n");
}else
fprintf(stderr,"dir exit !\n");
}
return 0;
}
linux c 文件,文件创建,数据复制
最新推荐文章于 2024-03-17 19:05:51 发布