標準I/O常用函數以及Linux檔案夾操作函數__Oracle

來源:互聯網
上載者:User

標準I/O(#include<stdio.h>)

1.        FILE*  fopen(const char *filename,const char *mode);

         參數:

                   filename:檔案名稱;

                   mode:模式:

                           

文字檔

二進位檔案

意義

r

rb

唯讀

w

wb

唯寫

a

ab

追加

r+

rb+

修改方式開啟

w+

wb+

修改方式開啟,並把檔案長度置為零

a+

ab+

修改方式開啟,在檔案後追加

2.        讀寫

size_t  fread(void *ptr, size_t  size, size_t nitem,  FILE*  stream);

size_t  fwrite(void *ptr, size_t  size, size_t nitem,  FILE*  stream);

參數:

         ptr:將要資料存放的字串

         size:沒錯讀取長度

nitem:操作次數

stream:檔案指標

3.        關閉檔案

int  flose(FILE* stream);

4.        將檔案流中未寫入的資料立刻寫入檔案中

int fflush(FILE  *stream);

5.        I/O中的lseek

int fseek(FILE  *stream, long intoffset, int whence);

參數:

stream:檔案指標;

         offset: 要設定檔案指標要的位置;

         whence:定義位移值的用法,可取下面值:

SEEK_SET

offset是一個絕對文章

SEEK_CUR

offset是一個相對於當前的相對位置

SEEK_END

offset是一個相對於檔案尾的相對位置

6.        單個擷取、輸入字元

int fgetc(FILE *steam);

int getc(FILE *steam);

int getchar();

int fputc(FILE *steam);

int putc(FILE *steam);

int putchar();

7.        擷取字串函數

char *fgets(char *s, int n, FILE* stream);

char *gets(char *s);

返回指向s的指標。

8.        格式化輸入輸出

int fprintf(FILE *stream, const char *format….);

int printf(const  char *format….);

int sprint(char *s, const  char*format….);

 

int scanf(const  char *format….);

int fscanf(FILE *stream, const char *format….);

int sscanf(char *s, const  char*format….);

9.        檔案流錯誤

#include <errno.h>

int ferror(FILE *stream);

int feof(FILE *stream);

void clearer(FILE *stream);

10.    字串拷貝

#include<string.h>

void*memcpy(void *dest, const void *src, size_t n);

函數返回dest的值。 目錄操作(#include <sys/types.h> #include <dirent.h>)

檔案夾結構體:

struct dirent{

   ino_t d_ino; //d_ino 此目錄進入點的inode

   ff_t d_off; //d_off 目錄檔案開頭至此目錄進入點的位移

   signed short int d_reclen; //d_reclen _name 的長度, 不包含NULL 字元

   unsigned char d_type; //d_type d_name 所指的檔案類型 d_name 檔案名稱

   char d_name[256];  檔案名稱

};

此部分,可參看檔案:“檔案夾操作.docx”

1.        開啟、讀、關閉目錄

DIR*opendir(const char *name);

structdirent *readdir(DIR *dirp);

intclosedir(DIR *dirp);

2.        返回當前位置

long int telldir(DIR *dirp);

3.        設定檔案夾指標

Void seekdir(DIR *dirp, long int loc);

4.        範例

#include<sys/types.h>

#include <dirent.h>

#include <unistd.h>

main()

{

    DIR * dir;

    struct dirent * ptr;

    int i;

    dir = opendir("/etc/rc.d");

    while((ptr = readdir(dir)) != NULL)

    {

        printf("d_name : %s\n",ptr->d_name);

    }

    closedir(dir);

}

5.        獲得檔案夾詳細資料

#include <sys/types.h>

#include <sys/stat.h>

#include <unistd.h>

int stat(const char *path, structstat *buf);

int fstat(int fd, struct stat*buf);

intlstat(const char *path, struct stat *buf); 路徑為連結時,擷取連結本身的資訊,而非連結指向的檔案的資訊。

Stat結構體:

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 512Bblocks 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 */

};

附:判斷檔案類型:

Structstat statbuf;

S_ISREG(statbuf.mode)  : is it a regular file?

S_ISDIR(statbuf.mode)  : directory?

S_ISCHR(statbuf.mode)  : character device?

S_ISBLK(statbuf.mode)  :block device?

S_ISFIFO(statbuf.mode) FIFO : (nastatbuf.modeedpipe)?

S_ISLNK(statbuf.mode)  : systatbuf.modebolic link? (Not inPOSIX.1-1996.)

S_ISSOCK(statbuf.mode) : socket?(Not in POSIX.1-1996.)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.