首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C 標準庫與系統函數詳解:F 開頭函數 (fabs, fclose, fopen, fprintf, fread, fseek, ftell 等)

     編輯:關於C語言

檔案 I/O 核心函數 (``) - 最常用

`fopen` / `freopen` / `fdopen` - 打開流


FILE *fopen(const char *path, const char *mode);
// mode: "r" "w" "a" "r+" "w+" "a+"  後綴 "b" (二進制) "t" (文本,默認)
// 返回: 流指針 / NULL (設 errno)

FILE *freopen(const char *path, const char *mode, FILE *stream);
// 重定向現有流 (常用於重定向 stdin/stdout/stderr)

FILE *fdopen(int fd, const char *mode);
// 將檔案描述符 (POSIX open 返回) 關聯為標準流

`fclose` / `fflush` - 關閉/刷新


int fclose(FILE *stream);
// 刷新緩衝區、關閉底層 fd、釋放結構體。返回 0/EOF

int fflush(FILE *stream);
// 輸出流: 寫緩衝區到設備。輸入流: 清除緩衝區 (行為未完全標準化)。
// stream=NULL: 刷新所有輸出流。

格式化 I/O: `fprintf` / `fscanf` / `sprintf` / `snprintf` / `vfprintf` 族


int fprintf(FILE *stream, const char *fmt, ...);
int fscanf(FILE *stream, const char *fmt, ...);
int sprintf(char *buf, const char *fmt, ...); // **危險**無長度檢查
int snprintf(char *buf, size_t n, const char *fmt, ...); // **安全**首選
// 返回: 寫入/讀取項數 / 負值錯誤
// snprintf 返回: 需要的字元數 (不含 \0),若 >= n 發生截斷

// 可變參數版 (實現自定義格式化函數)
int vfprintf(FILE *stream, const char *fmt, va_list ap);
int vfscanf(FILE *stream, const char *fmt, va_list ap);
int vsnprintf(char *buf, size_t n, const char *fmt, va_list ap);

字元/行 I/O: `fgetc` / `fputc` / `fgets` / `fputs` / `getc` / `putc`


int fgetc(FILE *stream); // 讀一字元 (函數)
int getc(FILE *stream);  // 通常為宏,速度快,stream 不可有副作用
int fputc(int c, FILE *stream);
int putc(int c, FILE *stream);

char *fgets(char *s, int n, FILE *stream);
// 讀最多 n-1 字元或遇 \n/EOF。**保留 \n**。返回 s/NULL
int fputs(const char *s, FILE *stream);
// 寫字串 (不含 \0),**不自動加 \n**

二進制塊 I/O: `fread` / `fwrite` (高效、結構體/陣列)


size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
// 讀 nmemb 個 size 字節項目。返回實際讀取項目數 (可能 < nmemb)

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
// 寫 nmemb 個 size 字節項目。返回實際寫入項目數

// 範例:寫入結構體陣列
struct Rec { int id; double val; } recs[100];
fwrite(recs, sizeof(struct Rec), 100, fp);
// 讀回
fread(recs, sizeof(struct Rec), 100, fp);

隨機訪問: `fseek` / `ftell` / `rewind` / `fgetpos` / `fsetpos`


int fseek(FILE *stream, long offset, int whence);
// whence: SEEK_SET(開頭), SEEK_CUR(當前), SEEK_END(結尾)
// 返回 0 成功, 非零失敗

long ftell(FILE *stream);
// 返回當前位置 (位元組偏移),-1L 失敗
// 大檔案 (>2GB) 需用 fgetpos/fsetpos (fpos_t 通常 64 位)

int fgetpos(FILE *stream, fpos_t *pos);
int fsetpos(FILE *stream, const fpos_t *pos);
// 可移植大檔案定位

void rewind(FILE *stream);
// 等價 fseek(stream, 0, SEEK_SET); clearerr(stream);

狀態檢查: `feof` / `ferror` / `clearerr` / `fileno`


int feof(FILE *stream);   // 檢查 EOF 標記 (讀操作嘗試讀過 EOF 後置位)
int ferror(FILE *stream); // 檢查錯誤標記
void clearerr(FILE *stream); // 清除 EOF 和錯誤標記
int fileno(FILE *stream); // 獲取底層檔案描述符 (POSIX 擴展,非標準 C)

正確讀取迴圈模式:


while (fgets(buf, sizeof(buf), fp)) { /* 處理 buf */ }
if (ferror(fp)) perror("Read error");
// feof(fp) 僅在循環結束後區分 EOF vs 錯誤

數學函數 (``, 連結 `-lm`)

`fabs` / `fabsf` / `fabsl` - 浮點絕對值


double fabs(double x); // 返回 |x|
// 編譯器常優化為單指令 (vabs.sd 等),無函數調用開銷

`floor` / `ceil` / `trunc` / `round` - 取整


double floor(double x);  // 向下取整 (不大於 x 最大整數)
double ceil(double x);   // 向上取整 (不小於 x 最小整數)
double trunc(double x);  // 向零截斷 (C99)
double round(double x);  // 四捨五入 (C99,半路情況離零方向)

`fmod` / `remainder` - 浮點餘數


double fmod(double x, double y); // x - trunc(x/y)*y (符號同 x)
double remainder(double x, double y); // x - round(x/y)*y (符號同 x, IEC 60559)

`frexp` / `ldexp` / `scalbn` - 指數尾數分解與縮放


double frexp(double x, int *exp); // x = m * 2^exp, 0.5 <= |m| < 1.0
double ldexp(double x, int exp);  // x * 2^exp
double scalbn(double x, int n);   // x * FLT_RADIX^n (通常 2^n)

字串轉換 (``)

`atof` / `atoi` / `atol` / `atoll` - 簡易轉換 (無錯誤檢查)


double atof(const char *nptr);
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr); // C99

`strtod` / `strtof` / `strtold` / `strtol` / `strtoul` / `strtoll` / `strtoull` - 完整轉換 (推薦)


double strtod(const char *nptr, char **endptr);
// endptr 指向第一個無效字元。若 endptr==nptr 則無轉換。
// errno: ERANGE (溢出/下溢)
// 基數 0: 自動識別 0x/0 前綴
long strtol(const char *nptr, char **endptr, int base);

記憶體操作 (`` / ``)

`free` - 釋放動態記憶體


void free(void *ptr);
// ptr 必須來自 malloc/calloc/realloc/aligned_alloc,或為 NULL (安全無操作)

`fread` / `fwrite` 已歸入檔案 I/O 章節

時間函數 (``)

`ftime` - 高精度時間 (舊式,POSIX.1-2001 標記過時,建議 `clock_gettime`)


#include 
void ftime(struct timeb *tp);
// struct timeb { time_t time; unsigned short millitm; short timezone; short dstflag; };

格式化輸出輔助 (``)

`fprintf` / `vfprintf` 已覆蓋

Windows 特有 `_f` 系列 (MSVC `` `` ``)

  • `_fopen`, `_fdopen`, `_freopen`:支援 `_SH_DENYRW` 等共享模式。
  • `_fstat`, `_fstat64`:獲取檔案狀態 (含 64 位大小)。
  • `_ftime`, `_ftime64` / `_ftime64_s`:時間獲取 (安全版)。
  • `_fullpath`:獲取絕對路徑。
  • `_future_library` 相關... (略)

總結:F 開頭函數分類速查

前綴功能域代表函數頭檔
`f` + 大寫標準流檔案操作`fopen`, `fclose`, `fread`, `fwrite`, `fseek`, `ftell`, `fprintf`, `fscanf`, `fgets`, `fputs`, `fgetc`, `fputc`, `feof`, `ferror`, `fflush`, `fileno`, `fgetpos`, `fsetpos`, `freopen`, `fdopen`, `fwide``stdio.h`
`f` + 小寫數學/轉換/記憶體/時間`fabs`, `floor`, `ceil`, `fmod`, `frexp`, `ldexp`, `fopen`(重複), `free`, `ftime``math.h`, `stdlib.h`, `time.h`, `sys/timeb.h`
`_f`MSVC 擴展/安全版`_fopen`, `_fdopen`, `_fstat`, `_ftime64_s`, `_fullpath``io.h`, `fcntl.h`, `stdio.h`, `share.h`
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved