文件操作:

欢迎加入QQ:498903810 一起交流、讨论知识,里面有大佬,也有小白,天下码农一家亲,大家一起讨论进步。

1、静态文件(Inode)

    硬盘中的文件,就是静态文件。每一个文件都是以多个块和多个扇区组成的,一般情况,一个扇区(512字节),64个扇区组成一个块。在硬盘中,对文件管理有一个特定的规则(文件管理表 + 真实的内容):文件管理表,这个表中是以文件为单位,提供了各个文件的 所有信息(每一个文件信息表就对应一个结构体,这个结构体称之为inode,也叫i节点,这个文件的包含的多少个块、多少扇区),、而我们通过查找这个表就可以找到我们所需要文件内容。
我们找文件,通过(文件名字)找的。第一步:在文件管理表中,中找到这个文件的名字
第二步:访问这个文件。U盘/硬盘格式化:1、快速格式化:清除了你的文件管理表,文件系统找不到你所    需要的文件名字。你的真实文件还在硬盘里,可以部分恢复。2、彻底格式化:把文件真实内容也清除掉了,你的U盘不能软件   技术恢复。必须借助国家安全机构(通过物理机制,通过硬件的记忆)。
联系:生活中,处理小文件的一个手段,文件压缩.把扇区的空余字节都利用起来,减少了占用硬盘上的空间。硬盘喜欢大文件。

2、动态文件 (Vnode)—>在内存中

一个程序的运行就是一个进程。而我们打开的文件就属于这个进程,而操作系统对于每一个进程都有一个结构体进行管理,这个管理当前进程所有
信息的结构体,我们就叫作(进程信息表)。这个表中有一个指针指向我们
的文件管理表,这个文件管理表就包含了本进程打开的所有文件,通过查找文件管理表的index(文件描述符fd,相当于这个结构体数组的下标),就得到了我们的文件所有信息的结构体(Vnode),而这个结构体的指针就是文件指针。

stat text.c 查看text.c文件的信息

IO:写入文件的缓冲区,减少了文件写入的频率,一般为4K大小

2.1文件属性

最近更改:2018-01-25 10:51:17.543987887 +0800
最近改动:2018-01-25 10:51:17.543987887 +0800最近更改指的是:文件的内容发生改变的时间。
最近改动指的是:文件的权限发生改变的时间。

3、文件与流

系统级别的文件操作函数:文件IO;标准库提供的操作文件函数:标准IO。区别在于:可移植性。文件IO可以完成对文件的所有操作,但是效率不高,
所以出现了使用标准IO;但是我们必须知道,标准IO最终也是通过文件IO实现的。

流:字符流的意思。读写文件的时候,是一个一个字符操作的连续进行。文件内容中是不分,行的仅仅连在一起的。

fwirte写进去的是数字,但是文件里没有数字,文件里放的是ASCII编码
fopen可以指定打开文件的格式
文件复制:1、连续打开同一个文件(inode)2、C语言里有两个API(dup, dup2)#include <unistd.h>int dup(int oldfd);//函数分配文件描述符int dup2(int oldfd, int newfd);//自己指定文件描述符用dup()创建的文件描述符,和旧的文件描述符号,各自操作自己的那份文件,但是两者的读写操作互相影响文件指针,相当于多次open加了O_APPEND标志。文件描述符:0:标准输入1:标准输出2:标准错误3、多个进程打开同一个文件

stat函数

//头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>//函数原型
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);//返回值
RETURN VALUEOn success, zero is returned.  On error, -1 is returned, and  errno  isset appropriately.//结构体
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 */
};//判断文件的类型
方法一:if(S_ISDIR(st.mode))S_ISREG(m)  is it a regular file?S_ISDIR(m)  directory?S_ISCHR(m)  character device?S_ISBLK(m)  block device?S_ISFIFO(m) FIFO (named pipe)?S_ISLNK(m)  symbolic link?  (Not in POSIX.1-1996.)S_ISSOCK(m) socket?  (Not in POSIX.1-1996.)方法二:if((st.st_mode & S_IFMT) == S_IFREG)S_IFMT     0170000   bit mask for the file type bit fieldsS_IFSOCK   0140000   socketS_IFLNK    0120000   symbolic linkS_IFREG    0100000   regular fileS_IFBLK    0060000   block deviceS_IFDIR    0040000   directoryS_IFCHR    0020000   character deviceS_IFIFO    0010000   FIFOS_ISUID    0004000   set-user-ID bitS_ISGID    0002000   set-group-ID bit (see below)S_ISVTX    0001000   sticky bit (see below)S_IRWXU    00700     mask for file owner permissionsS_IRUSR    00400     owner has read permissionS_IWUSR    00200     owner has write permissionS_IXUSR    00100     owner has execute permissionS_IRWXG    00070     mask for group permissionsS_IRGRP    00040     group has read permissionS_IWGRP    00020     group has write permissionS_IXGRP    00010     group has execute permissionS_IRWXO    00007     mask for permissions for others (not in group)S_IROTH    00004     others have read permissionS_IWOTH    00002     others have write permissionS_IXOTH    00001     others have execute permission

getpwuid函数

//头文件
#include <sys/types.h>
#include <pwd.h>//函数原型
struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);int getpwnam_r(const char *name, struct passwd *pwd,
char *buf, size_t buflen, struct passwd **result);int getpwuid_r(uid_t uid, struct passwd *pwd,
char *buf, size_t buflen, struct passwd **result);//结构体
struct passwd
{char   *pw_name;       /* username */char   *pw_passwd;     /* user password */uid_t   pw_uid;        /* user ID */gid_t   pw_gid;        /* group ID */char   *pw_gecos;      /* user information */char   *pw_dir;        /* home directory */char   *pw_shell;      /* shell program */
};

getgrgid函数

//头文件
#include <sys/types.h>
#include <grp.h>//函数原型
struct group *getgrnam(const char *name);
struct group *getgrgid(gid_t gid);int getgrnam_r(const char *name, struct group *grp,
char *buf, size_t buflen, struct group **result);int getgrgid_r(gid_t gid, struct group *grp,
char *buf, size_t buflen, struct group **result);//结构体
struct group
{char   *gr_name;       /* group name */char   *gr_passwd;     /* group password */gid_t   gr_gid;        /* group ID */char  **gr_mem;        /* group members */
};

time函数

//头文件
#include <time.h>//函数原型
time_t time(time_t *t);//返回值
RETURN VALUEOn  success,  the value of time in seconds since the Epoch is returned.On error, ((time_t) -1) is returned, and errno is set appropriately.

ctime函数

//头文件
#include <time.h>//函数原型
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);//传进去秒
char *ctime_r(const time_t *timep, char *buf);struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);time_t mktime(struct tm *tm);//结构体
struct tm
{int tm_sec;         /* seconds */int tm_min;         /* minutes */int tm_hour;        /* hours */int tm_mday;        /* day of the month */int tm_mon;         /* month */int tm_year;        /* year */int tm_wday;        /* day of the week */int tm_yday;        /* day in the year */int tm_isdst;       /* daylight saving time */
};The members of the tm structure are:tm_sec    The number of seconds after the minute, normally in the range 0 to 59, but can be up to 60 to allow for leap seconds.
tm_min    The number of minutes after the hour, in the range 0 to 59.
tm_hour   The number of hours past midnight, in the range 0 to 23.
tm_mday   The day of the month, in the range 1 to 31.
tm_mon    The number of months since January, in the range 0 to 11.
tm_year   The number of years since 1900.
tm_wday   The number of days since Sunday, in the range 0 to 6.
tm_yday   The number of days since January 1, in the range 0 to 365.tm_isdst  A  flag  that  indicates  whether  daylight saving time is in
effect at the time described.  The value is positive if  day‐
light  saving time is in effect, zero if it is not, and nega‐
tive if the information is not available.

strftime函数

//头文件
#include <time.h>//函数原型
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);

C语言linux下的文件操作(1)相关推荐

  1. Linux下对文件操作时出现乱码怎么办?

    Linux下对文件操作经常会遇见乱码问题,我在网上搜了一些解决方法,希望能对大家有所帮助. 如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中 ...

  2. linux下怎么批量命名文件,linux下的文件操作——批量重命名

    概述:在日常工作中,我们经常需要对一批文件进行重命名操作,例如将所有的jpg文件改成bnp,将名字中的1改成one,等等.文本主要为你讲解如何实现这些操作 1.删除所有的 .bak 后缀: renam ...

  3. c++ windows获得当前工作目录文件_基于linux下Python文件操作

    Python中的文件操作 1.文件的打开与关闭 想一想:如果想用word编写一份简历,应该有哪些流程呢? 1.打开word软件,新建一个word文件 2.写入个人简历信息 3.保存文件 4.关闭wor ...

  4. linux 文件操作函数,Linux下的文件操作函数及creat用法

    编写Linux应用程序要用到如下工具: (1)编译器:GCC GCC是Linux平台下最重要的开发工具,它是GNU的C和C++编译器,其基本用法为:gcc [options] [filenames]. ...

  5. linux下的文件操作IO

    前言 文件IO,又称系统IO,系统调用 打开 open 流程:1.创建一个.c在vim中编译 2.引入头文件 #include<unistd.h>#include<fcntl.h&g ...

  6. Linux下对文件的操作及添加新用户

    Linux下对文件的操作及添加新用户 一.对文件的操作 1.打包压缩文件 2.解压缩文件 3.对文件操作的其他命令 二.创建新用户 一.对文件的操作 1.打包压缩文件 2.解压缩文件 3.对文件操作的 ...

  7. C语言-------Linux下检测某个文件是否存在

    判断Linux下某个文件是否存在 以下是一个简单的 C 语言程序,用于判断 Linux 系统某个路径下是否存在某个文件: #include <stdio.h> #include <s ...

  8. linux下查看文件编码及修改编码

    linux下查看文件编码及修改编码 查看文件编码 在Linux中查看文件编码可以通过以下几种方式: 1.在Vim中可以直接查看文件编码 :set fileencoding 即可显示文件编码格式. 如果 ...

  9. linux下使用python操作mysql

    linux下使用python操作mysql 文章目录 linux下使用python操作mysql 一.Python 中MySQL的几个模块对象 1.模块 2.Connection 对象 3.Curso ...

最新文章

  1. 毕业后五年之内将决定你的一生
  2. 裂痕第一至五季/以法之名Damages迅雷下载
  3. Python3 爬虫实战 — 58同城武汉出租房【加密字体对抗】
  4. 云计算体系结构中soa构建层_云计算的服务模式及技术结构
  5. Task04:集合运算-表的加减法和join等-天池龙珠计划SQL训练营
  6. 机器学习算法总结之XGBoost(上)理论基础
  7. 阻塞IO, 非阻塞IO, 同步IO,异步IO
  8. 初步认识html以及表格的制作
  9. Hibernate级联删除时:Cannot delete or update a parent row: a foreign key constraint fails异常...
  10. Activity 设置SingleTask模式,当栈中已有Activity实例时的生命周期
  11. B2B行业使用什么CRM好? B2B行业的专属CRM—协同级CRM
  12. a[i]-‘0‘与(int)a[i]区别
  13. panic: runtime error: invalid memory address or nil pointer dereference
  14. JS脚本实现模拟按钮点击:批量抓取百度推广中的关键词建议
  15. 坑:解决64位2013word无法安装32位Visio的问题
  16. redis系列---让人懵逼的七连问
  17. PyCharm-professional-2016.2.3注册码
  18. 2022-2028年全球与中国电视发射机行业竞争格局与投资战略研究
  19. 浅谈图像处理与深度学习
  20. 诸葛亮为什么难以入围千古名相之列

热门文章

  1. java 单元测试_android开发奇技淫巧《通过main方法,写单元测试》
  2. python 类的定制_Python基础:20类的定制
  3. js匿名函数和具名函数
  4. 表情包简单制作和网站推荐
  5. 4G网关8305LN远程监控西门子触摸屏SMART 700IE ZLAN8305LN应用
  6. 充满科技感的词汇_科技展厅的定义
  7. 汽车系统/配件英文缩写合集
  8. Linux中vim里输出^A等方法
  9. IE8及其以下,特有js event对象坑
  10. linux平板系统下载官网,平板电脑安装Ubuntu教程