分類: linux
2014-07-31 20:02
13人閱讀收藏
舉報
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);
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.檔案拷貝(系統呼叫方式)
[cpp]view plain
copy
print?
#include
#include
#include
#include
int main()
#include #include #include #include int main()
[cpp]view plain
copy
print?
#include
#include
#include
#include
int main()
#include #include #include #include int main()
前者要比後者效率低很多很多,因為系統呼叫極其浪費cpu資源,應儘量減少系統呼叫的次數。
2.檔案拷貝(io庫函式方式)
[cpp]view plain
copy
print?
#include
#include
int main()
#include #include int main()
[cpp]view plain
copy
print?
#include
#include
int main()
#include #include int main()
linux檔案操作總結(下)
建立目錄 include include int mkdir const char path,mode t mode 目錄路徑 目錄許可權 若執行成功則返回0,失敗則返回 1,錯誤 存入errno刪除目錄 include include int mkdir const char path,mode ...
Linux下的檔案操作總結
in linux,everything is a file.3個主要的檔案描述符 0 標準輸入 1 標準輸出 2 標準錯誤 基本檔案操作 write 函式宣告 ssize t write int fildes,const void buf,size t nbytes 函式功能 把從buf開始的nby...
檔案操作總結
檔案操作 開啟 fopen 檔案指標名 fopen 檔名,使用檔案方式 file fp fp file a r 意義 在當前目錄下開啟檔案file a進行 讀 操作,並使fp指向該檔案。file fp fp c test rb 意義 開啟c盤的根目錄下的檔案test,這是乙個二進位制檔案,只允許按二...