linux不同文件函数调用,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

#include

#include

#include 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

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

#include

#include

#include

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

#include

#include

#include

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

#include

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

#include

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值