文本文件存儲多字段記錄(用戶|產品|價格...),字段間用 | 分隔。需求:文件第一行始終保存「總記錄數|總費用和」,每次追加記錄前更新首行統計。
文件頭覆寫需精確控制文件指針:
1. 讀取/計算新統計值
2. rewind(fp) 定位文件開頭
3. 寫入新統計行(長度可能變化)
4. fseek(fp, 0, SEEK_END) 定位文件末尾
5. 追加新記錄
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char Inf[3][20] = {0};
char strs[3][10];
int i = 0, iFeeSum = 0, iCount = 0, index = 0;
char lz_Recond[20] = {0}, temp[20] = {0};
// 模擬三條記錄:用戶|產品|費用
strcpy(Inf[0], "you|90002|100\n");
strcpy(Inf[1], "me|90003|100\n");
strcpy(Inf[2], "she|90004|100\n");
FILE *fp = fopen("Record.txt", "w");
if (!fp) { printf("Can not open file\n"); exit(-1); }
while (i < 3) {
strcpy(temp, Inf[i]);
char *p = strtok(temp, "|");
while (p) {
strcpy(strs[index++], p);
p = strtok(NULL, "|");
}
iFeeSum += atoi(strs[2]); // 累加費用(第 3 字段)
iCount++; // 記錄條數 +1
index = 0;
// 格式化統計行:總費用|總記錄數
sprintf(lz_Recond, "%d|%d\n", iFeeSum, iCount);
printf("lz_Recond=%s", lz_Recond);
rewind(fp); // 指針移至文件頭
fputs(lz_Recond, fp); // 覆寫統計行
fseek(fp, 0, SEEK_END); // 指針移至文件末尾
fputs(Inf[i], fp); // 追加新記錄
i++;
}
fclose(fp);
return 0;
}
| 函數 | 作用 | 注意事項 |
|---|---|---|
rewind(fp) | 等價 fseek(fp, 0, SEEK_SET),清除錯誤標誌 | 必須在寫入前調用,否則寫入位置不確定 |
fseek(fp, 0, SEEK_END) | 定位文件末尾 | 追加模式 "a" 自動定位,但此處需手動控制 |
strtok | 按分隔符分割字符串 | 破壞原字符串,故先 strcpy 到 temp |
sprintf(buf, "%-10d|%-10d\n", sum, cnt))// 統計行固定 20 字節:費用(10) + '|' + 記錄數(10) + '\n'
sprintf(lz_Recond, "%-10d|%-10d\n", iFeeSum, iCount);
// 寫入前確保文件最小長度
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
if (fsize < 20) {
char pad[20] = {0};
fseek(fp, 0, SEEK_SET);
fputs(" | \n", fp); // 佔位
fseek(fp, 0, SEEK_END);
}
// 正常覆寫流程
rewind(fp);
fputs(lz_Recond, fp);
fflush(fp); // 強制刷新緩衝區
fseek(fp, 0, SEEK_END);
fputs(Inf[i], fp);
flock/fcntl)