linux文件操作總結

Linux文件操作總結(系統調用和標準IO庫函數)

分類: linux 13人閱讀 評論(0)收藏 舉報

一、系統調用

❑ 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.文件拷貝(系統調用方式) 

  1. #include <unistd.h>  
  2. #include <sys/stat.h>  
  3. #include <fcntl.h>  
  4. #include <stdlib.h>  
  5. int main()  
  6. {  
  7.     char c;  
  8.     int in, out;  
  9.     in = open(“file.in”, O_RDONLY);  
  10.     out = open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);  
  11.     while(read(in,&c,1) == 1)  
  12.         write(out,&c,1);  
  13.     exit(0);  
  14. }  
#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);
}
  1. #include <unistd.h>  
  2. #include <sys/stat.h>  
  3. #include <fcntl.h>  
  4. #include <stdlib.h>  
  5. int main()  
  6. {  
  7.     char block[1024];  
  8.     int in, out;  
  9.     int nread;  
  10.     in = open(“file.in”, O_RDONLY);  
  11.     out = open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);  
  12.     while((nread = read(in,block,sizeof(block))) > 0)  
  13.         write(out,block,nread);  
  14.     exit(0);  
  15. }  
#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庫函數方式)

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. int main()  
  4. {  
  5.     int c;  
  6.     FILE *in, *out;  
  7.     in = fopen(“file.in”,”r”);  
  8.     out = fopen(“file.out”,”w”);  
  9.     while((c = fgetc(in)) != EOF)  
  10.         fputc(c,out);  
  11.     exit(0);  
  12. }  
#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);
}
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. int main()  
  5. {  
  6.     char buf[1024];  
  7.     int c;  
  8.     FILE *in, *out;  
  9.     in = fopen("file.in","r");  
  10.     out = fopen("file.out","w");  
  11.     while((c = fread(buf,1,1024,in)) != 0)  
  12.         fwrite(buf,1,c,out);  
  13.     exit(0);  
  14. }  
#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);
}
發佈了1 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章