access()

//检查是否调用进程有Access这个文件的权限,如果文件是一个符号链接,会将它解引用,成功返回0,失败返回-1设errno
#include <unistd.h>
int access(const char *pathname, int mode);
/*mode(Bitwise Or) :
F_OK //文件是否存在
R_OK //文件是否存在且授予了该进程读权限
W_OK //文件是否存在且授予了该进程写权限
X_OK //文件是否存在且授予了该进程执行权限
*/
if(0==access("./a.txt",F_OK))
printf("file exists\n");
if(0==access("./a.txt",R_OK))
printf("file exists and grants read permission\n");

fstat()、stat()、lstat()

//读取文件状态,fstat()从fd读取,stat () i从pathname读取,lstat()从pathname读取,如果文件是符号链接,则返回符号链接本身的信息,而其他的函数会对链接解引用,ls的底层实现就使用了lstat(),ls出的条目如果是符号链接会直接输出符号链接文件本身的信息
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
int fstat(int fd, struct stat *buf);
int stat (const char *pathname, struct stat *buf);
int lstat(const char *pathname, struct stat *buf);
/*
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */ //八进制usigned int o%
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 */ //ld%
blksize_t st_blksize; /* blocksize for filesystem 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 */ //ld%,秒
time_t st_ctime; /* time of last status change */
}; 一些POSIX宏可以用来检查文件类型,这些宏参数都是stat类型的st_mode成员
S_ISREG(m) //is it a regular file? //if yes, return 1
S_ISDIR(m) //is it directory?
S_ISCHR(m) //is it character device?
S_ISBLK(m) //is it block device?
S_ISFIFO(m) //is it FIFO (named pipe)?
S_ISLNK(m) //is it symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) //is it socket? (Not in POSIX.1-1996.) IPC对象也可以当做文件进而确定其类型,但他们的测试宏的参数是指向stat的指针,而不是st_mode成员
S_TYPEISMQ() //消息队列
S_TYPEISSEM() //信号量
S_TYPEISSHM() //共享内存对象 //这几个函数还提供了另外的方法读取文件的类型以及获取文件的权限 */

获取文件大小

  1. fseek()把offset移到SEEK_END, 再用ftell()返回文件的大小
  2. lseek() , 返回文件的大小
  3. stat(), struct stat st; st.st_size的数值就是文件大小
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
struct stat st={};
int res=stat("./a.txt",&st);
if(-1==res)
printf("stat"),exit(-1);
printf("st_mode=%o,st_size=%ld,st_mtime=%ld\n",st.st_mode,st.st_size,st.st_mtime);
if(S_ISREG(st.st_mode))
printf("Regular file\n");
if(S_ISDIR(st.st_mode))
printf("Dir file\n");
printf("file prot:%o\n",st.st_mode&0777);
printf("file size:%ld\n",st.st_size);
printf("latest modify:%s",ctime(&st.st_mtime));
struct tm* pt=localtime(&st.st_mtime);
printf("latest modify:%d-%d-%d-%02d:%02d:%02d\n",
1900+pt->tm_year,1+pt->tm_mon,pt->tm_mday,pt->tm_hour,pt->tm_min,pt->tm_sec);
//%02输出2个字符宽度,不足两位输出0站位
return 0;
}

chmod()、fchmod():

//更改文件的权限,这两个函数的唯一区别就是指定文件的方式不一样,成功返回0,失败返回-1,设errno
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);

truncate()、ftruncate():

//截断文件为指定大小,成功返回0,失败返回-1设errno
#include <unistd.h>
#include <sys/types.h>
int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
/*
如果原大小 > 指定大小,多余的文件数据被丢弃
如果原大小e < 指定大小,文件被扩展,扩展的部分被填充为'\0'
*/
/*-----------------------
* chmod(),truncate();
* -------------------*/
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
int res=chmod("a.txt",0600);
if(-1==res)
perror("chmod"),exit(-1); int res=truncate("a.txt",100);
if(-1==res)
perror("truncate"),exit(-1);
/*------------------------------------------------------
ftruncate();mmap();结构体指针初始化
----------------------------------------------------*/
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/mman.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
int id;
char name[20];
double salary;
}Emp;
int main(){
int fd=open("./emp.dat",O_RDWR|O_TRUNC|O_CREAT,0664);
if(-1==fd)
perror("open"),exit(-1);
printf("open success\n");
int res=ftruncate(fd,3*sizeof(Emp)); //要用sizeof,且是Emp(类型)不是emp(对象)
if(-1==res)
perror("ftruncate"),exit(-1); void* pv=mmap(NULL,3*sizeof(Emp),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if((void*)-1==pv)
perror("mmap"),exit(-1); Emp *pe=pv;
pe[0].id=1001;strcpy(pe[0].name,"zhangfei");pe[0].salary=3000;
pe[1].id=1002;strcpy(pe[1].name,"guanyu");pe[1].salary=3500;
pe[2].id=1003;strcpy(pe[2].name,"liubei");pe[2].salary=4000;
// *(pe+2)={1003,"liubei",4000};
// pe+2->salary=4000; //ATTENTION: ->优先级比+高, 所以一定要加()
printf("map success\n");
res=munmap(pv,3*sizeof(Emp));
if(-1==res)
perror("munmap"),exit(-1);
printf("unmap success\n");
res=close(fd);
if(-1==res)
perror("close"),exit(-1);
printf("close success\n");
return 0;
}

Linux文件和目录的更多相关文章

  1. Linux文件和目录权限详细讲解

    转载请标明出处: http://www.cnblogs.com/why168888/p/5965180.html 本文出自:[Edwin博客园] Linux文件和目录权限解读 如何设置Linxu文件和 ...

  2. Linux 文件与目录管理

    Linux 文件与目录管理 我们知道Linux的目录结构为树状结构,最顶级的目录为根目录 /. 其他目录通过挂载可以将它们添加到树中,通过解除挂载可以移除它们. 在开始本教程前我们需要先知道什么是绝对 ...

  3. CentOS(十)--与Linux文件和目录管理相关的一些重要命令②

    在结束了第二期的广交会实习之后,又迎来了几天休闲的日子,继续学习Linux.在上一篇随笔 Linux学习之CentOS(十七)--与Linux文件和目录管理相关的一些重要命令① 中,详细记录了与Lin ...

  4. CentOS(九)--与Linux文件和目录管理相关的一些重要命令①

       接上一篇文章,实际生产过程中的目录管理一定要注意用户是root 还是其他用户. 一.目录与路径 1.相对路径与绝对路径 因为我们在Linux系统中,常常要涉及到目录的切换,所以我们必须要了解 & ...

  5. Linux - 文件和目录常用命令

    文件和目录常用命令 目标 查看目录内容 ls 切换目录 cd 创建和删除操作 touch rm mkdir 拷贝和移动文件 cp mv 查看文件内容 cat more grep 其他 echo 重定向 ...

  6. linux文件与目录管理笔记

    ### Linux文件与目录管理 ---------- 绝对路径: / 相对路径:不以/开头的 当前目录 . 上一个工作目录 - 用户主目录 ~ root账户的主目录是/root 其他用户是/home ...

  7. linux文件权限目录配置笔记

    ###linux 文件权限目录配置笔记 ---------- 多人多任务环境 linux 一般将文件可存取的身份分为三个类别:owner group others Permission deny ls ...

  8. Linux文件与目录管理(一)

    一.Linux文件与目录管理 1.Linux的目录结构是树状结构,最顶级的目录是根目录/(用"/"表示) 2.Linux目录结构图: /bin:bin是Binary的缩写,这个目录 ...

  9. 【转】第七章、Linux 文件与目录管理

    原文网址:http://vbird.dic.ksu.edu.tw/linux_basic/0220filemanager.php 第七章.Linux 文件与目录管理 最近升级日期:2009/08/26 ...

  10. 第七章、Linux 文件与目录管理

    第七章.Linux 文件与目录管理   1. 目录与路径 1.1 相对路径与绝对路径 1.2 目录的相关操作: cd, pwd, mkdir, rmdir 1.3 关於运行档路径的变量: $PATH ...

随机推荐

  1. R语言-GA算法脚本

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  2. cnodejs社区论坛2--注册

  3. DBHelper (支持事务与数据库变更)

    1 概述 更新内容:添加 "支持数据分页" 这个数据库操作类的主要特色有 1>     事务操作更加的方便 2>     变更数据库更加的容易 3>  支持数据分 ...

  4. CSS3与页面布局学习笔记(一)——概要、选择器、特殊性与刻度单位

    web前端开发者最最注的内容是三个:HTML.CSS与JavaScript,他们分别在不同方面发挥自己的作用,HTML实现页面结构,CSS完成页面的表现与风格,JavaScript实现一些客户端的功能 ...

  5. 基于RulesEngine的业务规则实现

    规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策.接受数据输入,解释业务规则,并根据业务规则做出业务决策.比较常见 ...

  6. go语言常用函数:make

    创建数组切片 Go语言提供的内置函数make()可以用于灵活地创建数组切片.创建一个初始元素个数为5的数组切片,元素初始值为0: mySlice1 := make([]int, 5) 创建一个初始元素 ...

  7. swift学习笔记之-访问控制

    //访问控制 import UIKit /*访问控制(Access Control) 1.访问控制可以限定其他源文件或模块中的代码对你的代码的访问级别.这个特性可以让我们隐藏代码的一些实现细节,并且可 ...

  8. Android Studio git 版本回退到最新的版本

    1.场景 1.1 最新三次的提交 分别是:定义了一个变量k = 10 . 定义了一个变量 j = 6  . 定义了一个变量 i = 5 ; 本地仓库 和 远程仓库保持一致 1.2  我添加了一行代码 ...

  9. node.js Tools for Visual Studio 介绍

    node.js Tools for Visual Studio简称NTVS 项目 安装包地址:https://nodejstools.codeplex.com 目前支持2012和2013

  10. 有效解决 iOS The document “(null)” requires Xcode 8.0 or later.

    下载了一个 xocde8beta版本   运行之后   结果 在xcode7.3上再运行 就报这句错误   以下链接 是非常有效的解决办法 不信你试试 [链接]Thisversiondoesnotsu ...