一、系統調用
❑ 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 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);
- }
#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()
{
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);
- }
#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);
}