linux c文件相關操作

1,操作文件,需要文件句柄 ,頭文件 #include<stdio.h>

eg:

#include<stdio.h>


...

FILE *fp = fopen(filePath, mode);

fgets( str, length, fp );  //讀取length長度的內容到str緩存中

fclose(fp);

2,文件夾相關操作,頭文件 #include<dirent.h>

結構體dirent內容:

struct dirent
{
    long d_ino;                 /* inode number 索引節點號 */
    off_t d_off;                /* offset to this dirent 在目錄文件中的偏移 */
    unsigned short d_reclen;    /* length of this d_name 文件名長 */
    unsigned char d_type;       /* the type of d_name 文件類型 */
    char d_name [NAME_MAX+1];   /* file name (null-terminated) 文件名,最長256字符 */
}

3,刪除文件及文件夾函數:

#include<stdio.h>  有 remove函數, int remove(const char *filename) 

該函數可以傳入文件或文件夾,但只能刪除空文件夾,否則不成功。

有文章說,remove其實是根據傳入參數調用不同的函數,

若傳入文件,調用unlink函數

若傳入文件夾,調用rmdir函數

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