本文匯總 C 標準庫及常見擴展庫 (DOS、Graphics、IO) 中以 G 開頭的函數,涵蓋日期時間、圖形圖像、磁盤操作、環境變量、文件屬性、填充模式等領域,附帶函數原型、功能說明、參數詳解與可編譯運行範例,適合離線速查與教學參考。
| 分類 | 函數名 | 核心功能 | 所需頭文件 |
|---|---|---|---|
| 日期時間 | getdate | 獲取 DOS 系統日期 (年/月/日) | <dos.h> |
getftime | 獲取文件日期時間 (通過句柄) | <io.h>, <stdio.h> | |
| 圖形圖像 | getdefaultpalette | 獲取默認調色板定義 | <graphics.h> |
getdrivername | 獲取當前圖形驅動程序名稱 | <graphics.h> | |
getgraphmode | 返回當前圖形模式編號 | <graphics.h> | |
| 填充模式 | getfillpattern | 複製用戶定義填充模式到內存 | <graphics.h> |
getfillsettings | 獲取當前填充模式與顏色信息 | <graphics.h> | |
| 磁盤操作 | getdisk | 獲取當前默認驅動器號 (0=A, 1=B...) | <dir.h> |
| 磁盤信息 | getfat / getfatd | 獲取文件分配表 (FAT) 信息 (扇區/簇、密度) | <dos.h> |
| 內存傳輸 | getdta | 獲取磁盤傳輸地址 (DTA) | <dos.h> |
| 環境變量 | getenv | 從環境變量表獲取字符串值 | <stdlib.h> |
getdate — 獲取系統日期#include <dos.h>
#include <stdio.h>
int main() {
struct date d; // dos.h 定義: da_year, da_day, da_mon
getdate(&d);
printf("Year: %d, Month: %d, Day: %d\n", d.da_year, d.da_mon, d.da_day);
return 0;
}
注意:struct date 非標準 C,屬 DOS/Windows 特定擴展。跨平台請用 time.h 的 localtime。
getenv — 環境變量讀取 (標準 C、跨平台)#include <stdlib.h>
#include <stdio.h>
int main() {
char *comspec = getenv("COMSPEC"); // Windows: cmd.exe 路徑
if (comspec) printf("COMSPEC = %s\n", comspec);
else printf("Not found\n");
return 0;
}
getfat — 磁盤 FAT 信息識別 (教學經典)#include <dos.h>
#include <stdio.h>
int main() {
struct fatinfo info;
printf("請插入 A 盤軟盤...\n"); getchar();
getfat(1, &info); // 驅動器 1 = A:
switch (info.fi_fatid) {
case 0xFD: printf("360K 低密度\n"); break;
case 0xF9: printf("1.2M 高密度\n"); break;
default: printf("未格式化或未知\n");
}
if (info.fi_fatid == 0xFD || info.fi_fatid == 0xF9) {
printf("簇大小: %u 扇區, 簇總數: %u, 扇區字節: %u\n",
info.fi_sclus, info.fi_nclus, info.fi_bysec);
}
return 0;
}
getfillpattern / getfillsettings配合 setfillpattern 實現自定義位圖填充:
char pattern[8] = {0x00, 0x70, 0x20, 0x27, 0x25, 0x27, 0x04, 0x04};
setfillpattern(pattern, WHITE);
bar(0, 0, getmaxx(), getmaxy()); // 全屏填充
getfillpattern(pattern); // 讀回當前模式
pattern[4] -= 1; pattern[5] -= 3; // 修改模式
setfillpattern(pattern, WHITE);
bar(0, 0, getmaxx(), getmaxy()); // 再次填充觀察變化
getgraphmode / getdrivername — 運行時圖形環境探測int mode = getgraphmode();
char *name = getdrivername();
printf("Mode: %d (%s)\n", mode, name);
dos.h, graphics.h (BGI), dir.h 均為 Turbo C / Borland C / 早期 MS-DOS 時代產物,**現代編譯器 (GCC, Clang, MSVC) 不直接支持**。<chrono> (C++11) / <time.h>std::getenv / boost::nowide::getenv (Unicode)GetDiskFreeSpaceEx (WinAPI), statvfs (POSIX)