第5天

1. Linux文件操作相關函數(在 man 2(5)中查看函數用處)
        stat函數 *****
                穿透(追蹤)函數 -- 軟鏈接
        lstat函數
                不穿透(追蹤)
        access函數
        chmod函數
        chown函數      用戶組ID和用戶ID通過etc中passwd來獲取;
        truncate函數    ***
                文件長度 100
                第二個參數指定長度爲 20
                ..................  300 -> 文件被拓展
        鏈接函數
            link函數
            symlink函數
            readlink函數
            unlink函數(功能)創建零時文件,當我們將文件關掉之後。創建的文件會自動將文件刪掉。
 

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
    int fd = open("tempfile", O_CREAT | O_RDWR, 0755);
    if(fd == -1)
    {
        perror("open");
        exit(1);
    }

    int ret = unlink("tempfile");
    if(ret == -1)
    {
        perror("unlink");
        exit(1);
    }

    char buf[512];
    write(fd, "hello", 5);
    lseek(fd, 0, SEEK_SET);
    int len = read(fd, buf, sizeof(buf));
    write(STDOUT_FILENO, buf, len);

    close(fd);

    return 0;
}


filenum.c函數(計算文件目錄的大小)

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>

int getFileNum(char* root)
{
    // 打開目錄
    DIR* dir = opendir(root);
    if(dir == NULL)
    {
        perror("opendir");
        exit(0);
    }

    // 讀目錄
    int total = 0;
    char path[1024] = {0};
    struct dirent* ptr = NULL;
    while((ptr = readdir(dir)) != NULL)
    {
        // 跳過 . 和 ..
        if(strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)
        {
            continue;
        }
        // 判斷是不是文件
        if(ptr->d_type == DT_REG)
        {
            total ++;
        }
        // 如果是目錄
        if(ptr->d_type == DT_DIR)
        {
            // 遞歸讀目錄
            sprintf(path, "%s/%s", root, ptr->d_name);
            total += getFileNum(path);
        }
    }
    closedir(dir);
    return total;
}

int main(int argc, char* argv[])
{
    // 讀目錄, 統計文件個數
    int total = getFileNum(argv[1]);
    // 打印
    printf("%s has file number: %d\n", argv[1], total);
    return 0;
}

 

2. Linux目錄操作相關函數
3. fcntl 函數
        改變已經打開的文件的屬性
        
            打開文件的時候: 只讀
                    修改文件的: 添加追加 O_APPEND
                    
4. dup, dup2函數
        複製現有的文件描述符fd=open("a.txt",O_RDWR);


解決gcc編譯過程中c99語法報錯的問題
~/.bashrc
alias gcc='gcc -std=gnu99'

索引節點inode:保存的其實是實際的數據的一些信息,這些信息稱爲“元數據”(也就是對文件屬性的描述)。
例如:文件大小,設備標識符,用戶標識符,用戶組標識符,文件模式,擴展屬性,文件讀取或修改的時間戳,
鏈接數量,指向存儲該內容的磁盤區塊的指針,文件分類等等。
( 注意數據分成:元數據+數據本身 )

注意inode怎樣生成的:每個inode節點的大小,一般是128字節或256字節。inode節點的總數,在格式化時就給定
(現代OS可以動態變化),一般每2KB就設置一個inode。一般文件系統中很少有文件小於2KB的,所以預定按照2KB分,
一般inode是用不完的。所以inode在文件系統安裝的時候會有一個默認數量,後期會根據實際的需要發生變化。

注意inode號:inode號是唯一的,表示不同的文件。其實在Linux內部的時候,訪問文件都是通過inode號來進行的,
所謂文件名僅僅是給用戶容易使用的。當我們打開一個文件的時候,首先,系統找到這個文件名對應的inode號;然後,
通過inode號,得到inode信息,最後,由inode找到文件數據所在的block,現在可以處理文件數據了。

inode和文件的關係:當創建一個文件的時候,就給文件分配了一個inode。一個inode只對應一個實際文件,
一個文件也會只有一個inode。inodes最大數量就是文件的最大數量。


FILE* fp = open("file");

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章