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

文件操作:

歡迎加入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 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);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章