设为首页 加入收藏

TOP

Linux stat函数和stat命令(四)
2019-05-23 14:39:39 】 浏览:280
Tags:Linux stat 函数 命令
{ int fd = open("hello", O_WRONLY | O_CREAT, 0666); unlink("hello"); int ret = write(fd, "aaa", 4); if(ret > 0){ printf("write OK\n"); } }

12,chown函数:改变文件的所属用户和组

#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);
  • pathname:文件
  • owner:用户ID(数字的)/etc/passwd
  • group:组ID(数字的)/etc/group
  • 返回值:0成功,-1失败。

13,rename函数:重命名

#include <stdio.h>
int rename(const char *oldpath, const char *newpath);
  • oldpath :原来的文件名后者目录
  • newpath:新的文件名后者目录
  • 返回值:0成功,-1失败。

14,getcwd函数:获得当前工作的目录

#include <unistd.h>
char *getcwd(char *buf, size_t size);
  • buf:当前工作的目录
  • size:缓冲区大小
  • 返回值:
    • 成功返回当前工作的目录
    • 失败返回NULL

15,chdir函数:改变进程的工作目录

#include <unistd.h>
int chdir(const char *path);
  • path:目标工作目录
  • 返回值:0成功,-1失败

16,mkdir函数:创建目录

#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
  • pathname:目标工作目录
  • mode:mode & ~umask & 0777 。注意,如果没有x权限,则无法cd进入这个目录。
  • 返回值:0成功,-1失败

17,rmdir函数:删除目录,目录必须是空目录,也就是里面没有任何文件。

#include <unistd.h>
int rmdir(const char *pathname);

18,opendir函数:打开目录

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
  • name:目录名
  • 返回值:a pointer to the directory stream

19,readdir函数:读目录

#include <dirent.h>
struct dirent *readdir(DIR *dirp);

struct dirent {
  ino_t          d_ino;       /* Inode number */
  off_t          d_off;       /* Not an offset; see below */
  unsigned short d_reclen;    /* Length of this record */
  unsigned char  d_type;      /* Type of file; not supported
                                 by all filesystem types */
  char           d_name[256]; /* Null-terminated filename */
};
  • dirp:opendir函数的返回值
  • 返回值:结构体dirent,可以理解成最上面说的【目录项】
    • NULL代表读到末尾或者有错误
    • NULL以外代表目录项的内容

20,closedir函数:关闭目录

#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
  • dirp:opendir函数的返回值

21,strerron函数:打印出errno对应的文字信息。

#include <string.h>
char *strerror(int errnum);
  • errnum的宏放在文件:/usr/include/asm-generic/errno.h

例子:

#include <string.h>
#include <stdio.h>
#include <asm-generic/errno.h>//EDEADLK
int main(){
  char* buf = strerror(EDEADLK);
  printf("%s\n", buf);//Resource deadlock avoided
}

22,dup和dup2函数:文件描述符的重定向

#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
  • dup:和open类似,先打开一个新的文件描述符,让新的文件描述符也指向:oldfd指向的地方。
    • 成功返回新打开的文件描述符;失败返回-1.
  • dup2:
    • 先消除newfd的指向
    • 再让newfd指向oldfd指向的地方
    • 成功返回newfd;失败返回-1.

例子:调用printf2次,第一次printf把内容写到文件;第二次printf把内容打印到屏幕。

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(){

  int oldfd = dup(STDOUT_FILENO);
  int fd = open("www", O_WRONLY | O_CREAT, 0666);
  dup2(fd, STDOUT_FILENO);
  printf("aaaa\n");
  fflush(stdout);
  int ret = dup2(oldfd, STDOUT_FILENO);
  //int ret = dup2(oldfd, 6);
  //perror("dup2:");
  printf("reg:%d\n", ret);
  printf("aaaa\n");
  close(fd);
}

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇软件工程结对作业1 下一篇求给定精度的简单交错序列部分和

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目