Linux程式設計 標準IO(3)

2021-06-27 18:09:16 字數 2249 閱讀 2120

讀寫定位及格式化輸出

1. 讀寫定位函式

int fseek(file *stream, long offset, int whence);設定檔案讀寫位置

long ftell(file *stream);獲取檔案讀寫位置

void rewind(file *stream);回到檔案開頭

可處理檔案長度大於long

int fseeko(file *stream, off_t offset, int whence);

off_t ftello(file *stream);

ios c標準,支援跨平台

int fgetpos(file *stream, fpos_t *pos);設定檔案讀寫位置

int fsetpos(file *stream, fpos_t *pos);獲取檔案讀寫位位置

2. 格式化輸入輸出函式

格式化輸入:

#include

int scanf(const char *format, ...);//從標準io中輸入

int fscanf(file *stream, const char *format, ...);//從檔案中輸入

int sscanf(const char *str, const char *format, ...);//從字串

#include

int vscanf(const char *format, va_list ap);

int vsscanf(const char *str, const char *format, va_list ap);

int vfscanf(file *stream, const char *format, va_list ap);

(2)格式化輸出

#include

int printf(const char *format, ...);

int fprintf(file *stream, const char *format, ...);

int sprintf(char *str, const char *format, ...);

int snprintf(char *str, size_t size, const char *format, ...);

#include

int vprintf(const char *format, va_list ap);

int vfprintf(file *stream, const char *format, va_list ap);

int vsprintf(char *str, const char *format, va_list ap);

int vsnprintf(char *str, size_t size, const char *format, va_list ap);

3. 測試用例

#include

#include "scanftest.h"

void scantest()

;sscanf("123456", "%s", szbuf);

printf("data: %s\n", szbuf);

}

輸出:

1

data: 1,0,0

data: 123456

總結:

(1)scanf一次只能輸入乙個

(2)sscanf 能做到的,sprintf能做的更好

linux程式設計 標準IO

標準io簡介 1.標準io與三種緩衝區關聯 1 全緩衝 通過標準io對檔案 或裝置 進行操作時,通常緩衝區滿之後,才會進行實際的io操作 即寫到核心 對檔案進行操作時通常使用全緩衝。2 行緩衝 通過標準io對檔案 或裝置 進行操作時,通常緩衝區滿之後,或者遇到換行符時,才會進行實際的io操作 即寫到...

Linux標準I O程式設計

標準io的核心物件就是流 file結構體 1.流的開啟函式原型 file fopen const char path,const char mode 函式引數 path 要開啟的檔案路徑及檔名 mode 檔案開啟方式 函式返回值 成功 指向file指標 失敗 nullmode值 取值說明 r開啟唯讀...

I O 3 檔案操作

1 文字檔案編碼,文字檔案有不同的儲存方式,將字串以什麼樣的形式儲存為二進位制,這個就是編碼,utf 8 ascii unicode等,如果出現亂碼一般就是編碼的問題,文字檔案相關的函式一般都有乙個encoding型別的引數,取得編碼的方式 encoding.default encoding.utf...