标准IO用FILE 结构体来保存打开的文件信息;
文本流 和 二进制流(linux)typedefstruct_IO_FILE FILE;struct_IO_FILE{int _flags;/* High-order word is _IO_MAGIC; rest is flags. *//* The following pointers correspond to the C++ streambuf protocol. */char*_IO_read_ptr;/* Current read pointer */char*_IO_read_end;/* End of get area. */char*_IO_read_base;/* Start of putback+get area. */char*_IO_write_base;/* Start of put area. */char*_IO_write_ptr;/* Current put pointer. */char*_IO_write_end;/* End of put area. */char*_IO_buf_base;/* Start of reserve area. */char*_IO_buf_end;/* End of reserve area. *//* The following fields are used to support backing up and undo. */char*_IO_save_base;/* Pointer to start of non-current get area. */char*_IO_backup_base;/* Pointer to first valid character of backup area */char*_IO_save_end;/* Pointer to end of non-current get area. */struct_IO_marker*_markers;struct_IO_FILE*_chain;int _fileno;int _flags2;
__off_t _old_offset;/* This used to be _offset but it's too small. *//* 1+column number of pbase(); 0 is unknown. */unsignedshort _cur_column;signedchar _vtable_offset;char _shortbuf[1];
_IO_lock_t *_lock;#ifdef_IO_USE_OLD_IO_FILE};**** 标准IO减少系统调用的次数;
缓冲区就是一个应用层的内存块;
缓冲区作用于输入输出设备和cpu之间,为了使得低速的输入输出设备和高速的cpu工作相协调,避免输入输出设备占用cpu,使其高效率工作;***当缓冲区满的时候进行写操作,缓冲区空的时候读操作;
缓冲区类型://linux 只考虑输出缓冲区
全缓冲: 默认的缓冲类型 (默认缓冲区满的时候刷新)
行缓冲: 和终端相关,遇'\n'刷新,缓冲区满刷新
无缓冲: 标准错误流输出错误信息,直接输出到文件;
系统默认定义好的三个流:
标准输入 stdin0
标准输出 stdout1
标准错误 stderr2
缓冲区的刷新机制:(只考虑输出缓冲区)1.缓冲区满的时候刷新
2.针对遇行缓冲,遇到'\n';
3.当流被正常关闭的时候(程序正常结束),也会刷新
4.当遇到输入函数的时候,也会刷新;5.强制刷新 fflush();#include<stdio.h>intfflush(FILE *stream);{
功能: 强制刷新缓冲区;
参数: 文件流指针
返回值: 成功返回0,失败返回-1并设置错误号;}
#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>intstat(constchar*pathname,structstat*statbuf);//如果目标文件是符号链接文件那么是获取符号链接文件目标的属性intfstat(int fd,structstat*statbuf);//通过文件描述符获取文件属性intlstat(constchar*pathname,structstat*statbuf);//如果目标文件是符号链接文件,那么获取的是符号链接文件自己的属性
成功返回0识别返回-1并设置错误号;structstat{dev_t st_dev;/* ID of device containing file */ino_t st_ino;/* Inode number */mode_t st_mode;/* File type and mode */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;/* Block size for filesystem I/O */blkcnt_t st_blocks;/* Number of 512B blocks allocated *//* Since Linux 2.6, the kernel supports nanosecond
precision for the following timestamp fields.
For the details before Linux 2.6, see NOTES. */structtimespec st_atim;/* Time of last access */structtimespec st_mtim;/* Time of last modification */structtimespec st_ctim;/* Time of last status change */#definest_atimest_atim.tv_sec /* Backward compatibility */#definest_mtimest_mtim.tv_sec#definest_ctimest_ctim.tv_sec};
根据系统提供的宏来获取文件类型:
S_IFMT 0170000(类型掩码)
st_mode & S_IFMT
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_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.)
判断文件的权限
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
获取用户名:structpasswd*getpwuid(uid_t uid);
获取组名:structgroup*getgrgid(gid_t gid);
时间:
st_mtim 最后修改的时间---> localtime 转成本地时间
链接文件:
获取目标文件:#include<unistd.h>ssize_treadlink(constchar*pathname,char*buf,size_t bufsiz);
自己实现ls -l