From Gossip@caterpillar

C Gossip: 隨機存取檔案

使用 資料流游標,可以自由的移動至檔案中指定的位置進行讀取或寫入的動作,通常隨機存取檔案會使用二進位模式進行,文字模式開啟的檔案並不適合作隨機存取的動作。

如何利用隨機存取來讀寫所有的資料,必須視您的需求而定,需求決定您的資料結構,這邊以一個最簡單的例子來示範隨機存取,寫入檔案時都是使用固定大小的struct,由於資料大小固定,這可以方便明確的指定檔案中讀取的位置。

假設有一個簡單的學生成績資料如下:
#include <stdio.h> 
#include "Student.h"

int main(int argc, char* argv[]) {
if(argc != 2) {
puts("指令: create <filename>");
return 1;
}

FILE *file = fopen(argv[1], "wb");

if(!file) {
puts("檔案輸出失敗");
return 1;
}

int count;
printf("要建立幾筆資料? ");
scanf("%d", &count);

struct Student student = {0, "", 0.0};
int i;
for(i = 0; i < count; i++) {
fwrite((char*) &student, sizeof(student), 1, file);
}

fclose(file);

return 0;
}

執行結果:
create data.bin
要建立幾筆資料? 50

接下來可以使用下面這個程式進行隨機存取,使用學號來作資料的位置指定,將之儲存在檔案中的指定位置:
#include <stdio.h> 
#include "Student.h"

int main(int argc, char* argv[]) {
struct Student student;
int count = 0;

if(argc < 2) {
puts("指令: write <filename>");
return 1;
}

FILE *file = fopen(argv[1], "r+b");
if(!file) {
puts("無法讀取檔案");
return 1;
}

while(1) {
fread((char*) &student, sizeof(student), 1, file);
if(!feof(file)) {
count++;
}
else {
break;
}
}

rewind(file);

printf("輸入學號(1-%d)\n", count);
puts("輸入0離開");

while(1) {
printf("\n學號? ");
scanf("%d", &(student.studyNumber));
if(student.studyNumber == 0) {
break;
}

printf("輸入姓名 分數\n? ");
scanf("%s %lf", student.name, &(student.score));

fseek(file, (student.studyNumber - 1) * sizeof(student), SEEK_SET);
fwrite((char*) &student, sizeof(student), 1, file);
}

fclose(file);

return 0;
}

接下來可以使用下面這個程式讀取方才所輸入的資料:
#include <stdio.h> 
#include "Student.h"

int main(int argc, char* argv[]) {
struct Student student;
int count = 0, number;

if(argc != 2) {
puts("指令: read <filename>");
return 1;
}

FILE *file = fopen(argv[1], "r");
if(!file) {
puts("無法讀取檔案");
return 1;
}

while(1) {
fread((char*) &student, sizeof(student), 1, file);
if(!feof(file)) {
count++;
}
else {
break;
}
}
rewind(file);

printf("輸入學號(1-%d)\n", count);
puts("輸入0離開");

while(1) {
printf("\n學號? ");
scanf("%d", &number);
if(number == 0) {
break;
}
else if(number > count) {
printf("輸入學號(1-%d)\n", count);
continue;
}

puts("\n學號\t姓名\t\t分數");

fseek(file, (number - 1) * sizeof(student), SEEK_SET);
fread((char*) &student, sizeof(student), 1, file);
printf("%d\t%s\t%f\n",
student.studyNumber, student.name, student.score);
}

fclose(file);

return 0;
}

執行結果:
read data.bin
輸入學號(1-50)
輸入0離開
學號? 1

學號    姓名            分數
1       良葛格          88

學號? 2

學號    姓名            分數
2       毛妹妹          94

學號? 3

學號    姓名            分數
0                       0

學號? 5

學號    姓名            分數
5       毛毛蟲          75

學號? 0

這幾個程式是隨機存取的簡單示範,您也可以結合起來,製作一個簡易的成績登記程式。