dup ,dup2,fcntl,ioctl系统调用

 . dup ,dup2 函数
 int dup(int oldfd)
 int dup(int oldfd , int newfd)

dup用来复制参数oldfd的文件描述符
duo2可以用newfd来指定新文件描述符的数值

2.fcntl函数
对已经打开的文件描述符进行各种控制操作,以及改变已经打开文件的各种属性

3.ioctl函数
控制设备,不能用其他函数进行控制操作都可以用ioctl来进行

3.文件属性的操作
 1.  shell里可通过ls来获取文件属性,那么在程序里怎么获取文件属性呢?
         用到stat/fstat/lstat

         man  stat
          #include<sys/types.h>
          #include<sys/stat.h>
          #include<unistd.h>
          int stat(const char *file_name,struct stat *buf);
          int fstat(int filedes,struct stat *buf);
          int lstat(const char *fime_name ,struct stat *buf);

stat 获取由参数file_name指定的文件名的状态信息,保存参数到struct stat *buf中
         fstat   与stat 区别,fstat通过文件描述符来指定文件
         lstat  与stat,对于符号连接文件,lstat返回的是符号连接文件本身的状态信息。而stat返回的是符号连接文件指向的文件的状态信息。

            struct stat {
                dev_t     st_dev;     /* ID of device containing file */
                ino_t     st_ino;     /* inode number */
                mode_t    st_mode;    /* protection */
                nlink_t   st_nlink;   /* number of hard links */
                uid_t     st_uid;     /* user ID of owner */
                gid_t     st_gid;     /* group ID of owner */
                dev_t     st_rdev;    /* device ID (if special file) */
                off_t     st_size;    /* total size, in bytes */
                blksize_t st_blksize; /* blocksize for file system I/O */
                blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
                time_t    st_atime;   /* time of last access */
                time_t    st_mtime;   /* time of last modification */
                time_t    st_ctime;   /* time of last status change */
            };

eg:获取文件属性
                                                                                                                                                                                              79,0-1        Bot

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

 int main(int argc , char * argv[]){
         struct stat buf;
         /*check param number*/
         ){
                 printf("Usage mystat  <file_name>\n");
                 exit();
         }
         /*get file attr*/
         ],&buf) == -){
                 perror("stat:");
                 exit();
         }
         //print file attr
         printf("device is :%d\n",buf.st_dev);
         printf("inode is :%d\n",buf.st_ino);
         printf("mode is :%d\n",buf.st_mode);
         printf("number of hard links  is :%d\n",buf.st_nlink);
         printf("user ID of owner  is :%d\n",buf.st_uid);
         printf("group ID of owner  is :%d\n",buf.st_gid);
         printf("device type (if inode device ) is :%d\n",buf.st_rdev);

         printf("total size ,in bytes  is :%d\n",buf.st_size);
         printf("blocksize for filesystem I/O  is :%d\n",buf.st_blksize);
         printf("number of blocks allocated is :%d\n",buf.st_blocks);

         printf("time of last access  is :%s\n",ctime(&buf.st_atime));
         printf("time of last modification  is :%s\n",ctime(&buf.st_mtime));
         printf("time of last change  is :%s\n",ctime(&buf.st_ctime));

         ;

 }
 output :
 [fubin@localhost C]$ ./my_chmod  example.c
 device
 inode
 mode
 number of hard links
 user ID of owner
 group ID of owner
 device type (
 total size ,
 blocksize
 number of blocks allocated
 time of last access   ::
 time of last modification   ::
 time of last change   :: 

2.设置文件属性

 chmod/fchmod , chown/fchown/lchown,truncate/ftruncate,utime,umask
 .chmod/fchmod
 修改文件的存取权限
 .chown/fchown/lchown
 修改文件的用户id和组id
 .ftruncate/truncate
 改变文件大小
 .utime
 改变文件的st_mtime和st_ctime域,即存取时间和修改时间。
 .umask
 使用文件创建时使用的屏蔽字

屏蔽文件权限eg:

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

 int main(){

         umask();// don't shield any permission
         ){
                 perror("creat");
                 exit();
         }

         umask(S_IRWXO);// shield other user's permission
         ){
                 perror("creat");
                 exit();
         }

         ;
 }

 output:
 -rwxrwx---.  fubin fubin     Jan   : umasktest1.txt
 -rwxrwxrwx.  fubin fubin     Jan   : umasktest.txt
     

3.文件的移动和删除
  1.文件的移动

 rename 修改文件名或移动文件位置
   #include <stdio.h>
     int rename(const char *oldpath, const char *newpath);
   将oldpath文件夹名改为newpath,若newpath存在,则源文件会被删除
   用rename实现简单linux下mv的功能
   #include<stdio.h>
   #include<stdlib.h>
   int main(int argc , char **argv){

         ){
                 perror("my_mv <oldpath> <newpath>");
                 exit();
         }

         ],argv[]) < ) {
                 perror("rename");
                 exit();
         }

         ;
 }

 incompatible implicit declaration of built-in function 'exit'
 警告:exit函数在stdlib库中,需要包含#include<stdlib.h>进来

2.文件的删除

 unlink  文件的删除
 rmdir   目录的删除
 remove 封装了unlink和rmdir

 unlink:文件的链接数为0且没有进程打开这个文件,文件被删除且其占用的磁盘空间被释放。
 //如果文件的链接数为0但是有进程打开了这个文件,文件暂时不删除,知道所有打开这个文件的进程结束时文件才被删除。
 使用这一点可以确保程序崩溃时,他所创建的临时文件也不会保留。
        #include <unistd.h>
        int unlink(const char *pathname);
  //参数pathname指向一个符号链接,连接被删除。若 指向一个套接字(socket),FIFO(命名管道),设备文件,该名字被删除,但已经打开这个文件的进程仍然可以使用。
  

3.目录的创建和删除
  1.目录的创建 mkdir
  2.目录的删除 rmdir

Linux C 程序 文件属性,文件删除(15)的更多相关文章

  1. linux应用程序开发-文件编程-系统调用方式

    在看韦东山视频linux驱动方面有一些吃力,究其原因,虽然接触过linux应用程序编程,但是没有深入去理解,相关函数用法不清楚,正好看到国嵌视频对这一方面讲的比较透彻, 所以把学习过程记录下来,也作为 ...

  2. linux应用程序开发-文件编程-库函数

    ---恢复内容开始--- c库函数文件操作独立于具体的系统平台,可移植性较好. 库函数-创建和打开 FILE*fopen(const char*filename,const char*mode) mo ...

  3. linux中/etc/fstab文件删除或修改了,导致系统无法启动

    在linux中,/etc/fstab文件是磁盘挂载的问题,若该文件不小心给修改了,或者被删除了,那么就会导致系统无法重启.因为/etc/fstab文件是记录磁盘挂载的信息,若该文件出现了问题,那么对应 ...

  4. linux文件属性之linux文件删除原理

    Linux是通过link的数量来控制文件删除的,只有当一个文件不存在任何link的时候,这个文件才会被删除.一般来说,每个文件都有2个link计数器:i_count和i_nlink. i_count的 ...

  5. Linux文件属性之Linux文件删除重要原理详解

    Linux下文件删除的原理 只要dongdaxiafile(源文件).服务进程.dongdaxiaflie_hard_link(硬链接文件)三个中的任意一个存在文件不会被删除.

  6. day59:Linux:编辑工具vim&文件类型&文件属性

    目录 1.Linux编辑工具vim 2.Linux文件类型 3.Linux文件属性 4.今日份Linux练习题 Linux编辑工具vim 1.什么是vim 文本文件的编辑工具,  和windows的n ...

  7. 【Linux】rsync同步文件 & 程序自启动

    rsync使用 1. 为什么使用rsync? rsync解决linux系统下文件同步时, 增量同步问题. 使用场景: 线上需要定时备份数据文件(视频资源), 使用rsync完成每天的增量备份. 参见: ...

  8. 软硬链接、文件删除原理、linux中的三种时间、chkconfig优化

    第1章 软硬链接 1.1 硬链接 1.1.1 含义 多个文件拥有相同的inode号码 硬链接即文件的多个入口 1.1.2 作用 防止你误删除文件 1.1.3 如何创建硬链接 ln 命令,前面是源文件  ...

  9. linux文件删除原理

    文件删除的原理 linux的文件名是存在父目录的block里面的,并指向这个文件的inode节点,这个文件的inode节点在标记指向存放这个文件的block的数据块.我们删除文件,实际上不是清除ino ...

随机推荐

  1. Playing with ptrace, Part II

    Playing with ptrace, Part II Issue From Issue # December Dec , By Pradeep Padala inSysAdmin In Part ...

  2. Date Format, 时间戳格式化

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...

  3. 循环语句until和while

    一.until语句的基本格式 until 条件测试 do 语句块 done 只要条件测试语句未成功结束,则执行语句块.(如果一开始条件测试语句就成功退出,那么一次也不执行语句块.这里跟C语言中的do. ...

  4. php常见问题辨析(二)

    一:unset: 1.如果在函数中 unset() 一个全局变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用 unset() 之前一样的值,如果您想在函数中 unset() 一个全局变量,可 ...

  5. eclipse 的小技巧

    1. ctrl+o:快速outline 如果想要查看当前类的方法或某个特定方法,但又不想把代码拉上拉下,也不想使用查找功能的话,就用ctrl+o吧.它可以列出当前类中的所有方法及属性,你只需输入你想要 ...

  6. Roadblocks http://poj.org/problem?id=3255

    Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best ...

  7. [改善Java代码]注意Class类的特殊性

    Java语言是先把Java源文件编译成后缀为class的字节码文件,然后再通过ClassLoader机制把这些类文件加载到内存中,最后生成实例执行的,这是Java处理的基本机制,但加载到内存中的数据是 ...

  8. 【基本计数方法---加法原理和乘法原理】UVa 11538 - Chess Queen

    题目链接 题意:给出m行n列的棋盘,当两皇后在同行同列或同对角线上时可以互相攻击,问共有多少种攻击方式. 分析:首先可以利用加法原理分情况讨论:①两皇后在同一行:②两皇后在同一列:③两皇后在同一对角线 ...

  9. linux 第一次获得root权限

    开机进入桌面,ctrl+alt+T打开终端————在此时终端显示的是 用户名@电脑名:-$   表示普通用户   在此处输入:sudo passwd root   此时提示———— [sudo] pa ...

  10. 关于IE6的一些常见的CSS BUG处理

    CSS BUG:样式在各浏览器中解析不一致的情况,或者说CSS样式在浏览器中不能正确显示的问题称为CSS BUG: CSS Hack:css hack是指一种兼容css 在不同浏览器中正确显示的技巧方 ...