您的位置:首页 > 运维架构 > Linux

C语言linux下的文件操作(1)

2018-01-26 15:38 471 查看

文件操作:

欢迎加入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、文
d3e6
件与流

系统级别的文件操作函数:文件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 VALUE
On success, zero is returned.  On error, -1 is returned, and  errno  is
set 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 fields
S_IFSOCK   0140000   socket
S_IFLNK    0120000   symbolic link
S_IFREG    0100000   regular file
S_IFBLK    0060000   block device
S_IFDIR    0040000   directory
S_IFCHR    0020000   character device
S_IFIFO    0010000   FIFO
S_ISUID    0004000   set-user-ID bit
S_ISGID    0002000   set-group-ID bit (see below)
S_ISVTX    0001000   sticky bit (see below)
S_IRWXU    00700     mask for file owner permissions
S_IRUSR    00400     owner has read permission
S_IWUSR    00200     owner has write permission
S_IXUSR    00100     owner has execute permission
S_IRWXG    00070     mask for group permissions
S_IRGRP    00040     group has read permission
S_IWGRP    00020     group has write permission
S_IXGRP    00010     group has execute permission
S_IRWXO    00007     mask for permissions for others (not in group)
S_IROTH    00004     others have read permission
S_IWOTH    00002     others have write permission
S_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 VALUE
On  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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: