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 返回) 關聯為標準流
int fclose(FILE *stream);
// 刷新緩衝區、關閉底層 fd、釋放結構體。返回 0/EOF
int fflush(FILE *stream);
// 輸出流: 寫緩衝區到設備。輸入流: 清除緩衝區 (行為未完全標準化)。
// stream=NULL: 刷新所有輸出流。
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);
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**
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);
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);
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 錯誤
double fabs(double x); // 返回 |x|
// 編譯器常優化為單指令 (vabs.sd 等),無函數調用開銷
double floor(double x); // 向下取整 (不大於 x 最大整數)
double ceil(double x); // 向上取整 (不小於 x 最小整數)
double trunc(double x); // 向零截斷 (C99)
double round(double x); // 四捨五入 (C99,半路情況離零方向)
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)
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)
double atof(const char *nptr);
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr); // C99
double strtod(const char *nptr, char **endptr);
// endptr 指向第一個無效字元。若 endptr==nptr 則無轉換。
// errno: ERANGE (溢出/下溢)
// 基數 0: 自動識別 0x/0 前綴
long strtol(const char *nptr, char **endptr, int base);
void free(void *ptr);
// ptr 必須來自 malloc/calloc/realloc/aligned_alloc,或為 NULL (安全無操作)
#include
void ftime(struct timeb *tp);
// struct timeb { time_t time; unsigned short millitm; short timezone; short dstflag; };
| 前綴 | 功能域 | 代表函數 | 頭檔 |
|---|---|---|---|
| `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` |