Linux文件操作总结(系统调用和标准IO库函数)

  • Post author:
  • Post category:linux




一、系统调用


❑ 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);
}



版权声明:本文为jipingyuan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。