1. 程式人生 > >Linux檔案操作總結(系統呼叫和標準IO庫函式)

Linux檔案操作總結(系統呼叫和標準IO庫函式)

一、系統呼叫

❑ open: Open a file or device
❑ read: Read from an open file or device
❑ write: Write to a file or device
❑ close: Close the file or device
❑ ioctl: Pass control information to a device driver

函式原型:#include <unistd.h>

#include <fcntl.h>

#include <sys/types.h>

#include <sys/stat.h>


int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);

(mode=O_RDONLY  |  O_WRONLY  |  O_RDWR)
size_t write(int fildes, const void *buf, size_t nbytes);

size_t read(int fildes, void *buf, size_t nbytes);

int close(int fildes);

int ioctl(int fildes, int cmd, ...);

off_t lseek(int fildes, off_t offset, int whence);

此處whence有三個引數可選:SEEK_SET、SEEK_CUR、SEEK_END

SEEK_SET:是一個絕對值,相對檔案頭的offset位置;

SEEK_CUR:是相對於當前檔案讀寫指標的offset相對位置;

SEEK_END:是相對於檔案末尾的offset相對位置。

❑ SEEK_SET: offset is an absolute position
❑ SEEK_CUR: offset is relative to the current position
❑ SEEK_END: offset is relative to the end of the file

int fstat(int fildes, struct stat *buf);
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);

二、標準IO庫函式

❑ fopen, fclose
❑ fread, fwrite
❑ fflush
❑ fseek
❑ fgetc, getc, getchar
❑ fputc, putc, putchar
❑ fgets, gets
❑ printf, fprintf, and sprintf
❑ scanf, fscanf, and sscanf

函式原型:

#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);

❑ “r” or “rb”: Open for reading only
❑ “w” or “wb”: Open for writing, truncate to zero length
❑ “a” or “ab”: Open for writing, append to end of file,追加方式,將要新增的內容追加在已有的部分後面。
❑ “r+” or “rb+” or “r+b”: Open for update (reading and writing),如果想要開啟一個檔案,先讀取裡面的內容,再更新原來的內容,可以用此方式。
❑ “w+” or “wb+” or “w+b”: Open for update, truncate to zero length,如果想要開啟一個檔案,先寫此檔案,再讀此檔案,可以用此方式。
❑ “a+” or “ab+” or “a+b”: Open for update, append to end of file,開啟一個檔案,可讀可寫,寫的話,是以追加方式寫在檔案末尾的。

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

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

int fclose(FILE *stream);

int fflush(FILE *stream);

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

此處whence有三個引數可選:SEEK_SET、SEEK_CUR、SEEK_END

SEEK_SET:是一個絕對值,相對檔案頭的offset位置;

SEEK_CUR:是相對於當前檔案讀寫指標的offset相對位置;

SEEK_END:是相對於檔案末尾的offset相對位置。

❑ SEEK_SET: offset is an absolute position
❑ SEEK_CUR: offset is relative to the current position
❑ SEEK_END: offset is relative to the end of the file

int fgetc(FILE *stream);
int getc(FILE *stream);
int getchar();

int fputc(int c, FILE *stream);
int putc(int c, FILE *stream);
int putchar(int c);

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

int printf(const char *format, ...);
int sprintf(char *s, const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);

int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *s, const char *format, ...);

三、例程

1.檔案拷貝(系統呼叫方式) 

#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
    char c;
    int in, out;
    in = open(“file.in”, O_RDONLY);
    out = open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
    while(read(in,&c,1) == 1)
        write(out,&c,1);
    exit(0);
}
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
    char block[1024];
    int in, out;
    int nread;
    in = open(“file.in”, O_RDONLY);
    out = open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
    while((nread = read(in,block,sizeof(block))) > 0)
        write(out,block,nread);
    exit(0);
}

前者要比後者效率低很多很多,因為系統呼叫極其浪費CPU資源,應儘量減少系統呼叫的次數。
2.檔案拷貝(IO庫函式方式)

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int c;
    FILE *in, *out;
    in = fopen(“file.in”,”r”);
    out = fopen(“file.out”,”w”);
    while((c = fgetc(in)) != EOF)
        fputc(c,out);
    exit(0);
}
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char buf[1024];
    int c;
    FILE *in, *out;
    in = fopen("file.in","r");
    out = fopen("file.out","w");
    while((c = fread(buf,1,1024,in)) != 0)
        fwrite(buf,1,c,out);
    exit(0);
}