#include <stdio.h>
#include <string.h>
typedef struct {
int id;
char name[50];
float score;
} Student;
typedef struct {
char c;
int i;
} Mixed;
#pragma pack(push, 1)
typedef struct {
char c;
int i;
} PackedMixed;
#pragma pack(pop)
static int copy_binary_file(const char *src, const char *dst, size_t *total_bytes) {
char buf[64];
FILE *in = fopen(src, "rb");
if (!in) return -1;
FILE *out = fopen(dst, "wb");
if (!out) {
fclose(in);
return -1;
}
size_t copied = 0, n = 0;
while ((n = fread(buf, 1, sizeof(buf), in)) > 0) {
if (fwrite(buf, 1, n, out) != n) {
fclose(in);
fclose(out);
return -1;
}
copied += n;
}
int err = ferror(in);
fclose(in);
fclose(out);
if (err) return -1;
*total_bytes = copied;
return 0;
}
int main(void) {
/* Case 1: text write (fprintf/fputs) + fscanf/fgets read behavior */
const char *txt_path = "/tmp/c_fileio_text.txt";
FILE *fp = fopen(txt_path, "w+");
char w1[32] = {0}, r2[64] = {0}, r3[64] = {0};
int c1_pass = 0;
if (fp != NULL) {
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fflush(fp);
rewind(fp);
int matched = fscanf(fp, "%31s", w1);
char *p2 = fgets(r2, (int)sizeof(r2), fp);
char *p3 = fgets(r3, (int)sizeof(r3), fp);
fclose(fp);
c1_pass = (matched == 1 && p2 != NULL && p3 != NULL &&
strcmp(w1, "This") == 0 &&
strcmp(r2, " is testing for fprintf...\n") == 0 &&
strcmp(r3, "This is testing for fputs...\n") == 0);
}
remove(txt_path);
/* Case 2: binary struct & array fwrite/fread + EOF detection */
const char *stu_path = "/tmp/c_fileio_student.bin";
Student s_out = {101, "Alice", 95.5f};
Student s_in = {0};
int eof_after_short = 0;
size_t short_cnt = 0;
fp = fopen(stu_path, "wb");
if (fp != NULL) {
fwrite(&s_out, sizeof(Student), 1, fp);
fclose(fp);
}
fp = fopen(stu_path, "rb");
if (fp != NULL) {
Student buf[3];
short_cnt = fread(buf, sizeof(Student), 3, fp);
eof_after_short = feof(fp) != 0 && ferror(fp) == 0;
if (short_cnt >= 1) s_in = buf[0];
fclose(fp);
}
remove(stu_path);
int c2_pass = (short_cnt == 1 && eof_after_short &&
s_in.id == 101 && strcmp(s_in.name, "Alice") == 0 && s_in.score == 95.5f);
/* Case 3: chunked binary file copy + alignment comparison */
const char *src_bin = "/tmp/c_fileio_arr.bin";
const char *dst_bin = "/tmp/c_fileio_arr_copy.bin";
int arr_in[5] = {1, 2, 3, 4, 5};
int arr_out[5] = {0};
size_t copied = 0;
fp = fopen(src_bin, "wb");
if (fp != NULL) {
fwrite(arr_in, sizeof(int), 5, fp);
fclose(fp);
}
int copy_ok = (copy_binary_file(src_bin, dst_bin, &copied) == 0);
fp = fopen(dst_bin, "rb");
if (fp != NULL) {
size_t got = fread(arr_out, sizeof(int), 5, fp);
if (got != 5) copy_ok = 0;
fclose(fp);
}
remove(src_bin);
remove(dst_bin);
int arr_match = (memcmp(arr_in, arr_out, sizeof(arr_in)) == 0);
size_t sz_mixed = sizeof(Mixed);
size_t sz_packed = sizeof(PackedMixed);
int c3_pass = (copy_ok && arr_match && copied == 5 * sizeof(int) && sz_mixed == 8 && sz_packed == 5);
/* Case 4: fseek + ftell random access on record file */
const char *rec_path = "/tmp/c_fileio_records.bin";
Student batch[10];
for (int i = 0; i < 10; i++) {
memset(&batch[i], 0, sizeof(Student));
batch[i].id = 101 + i;
snprintf(batch[i].name, sizeof(batch[i].name), "User%d", i + 1);
batch[i].score = 80.0f + (float)i;
}
fp = fopen(rec_path, "wb");
if (fp != NULL) {
fwrite(batch, sizeof(Student), 10, fp);
fclose(fp);
}
long file_size = -1, record_count = -1;
Student picked = {0};
fp = fopen(rec_path, "rb");
if (fp != NULL) {
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
record_count = file_size / (long)sizeof(Student);
fseek(fp, (long)(sizeof(Student) * 5), SEEK_SET);
if (fread(&picked, sizeof(Student), 1, fp) != 1) {
picked.id = -1;
}
fclose(fp);
}
remove(rec_path);
int c4_pass = (file_size == (long)(10 * sizeof(Student)) && record_count == 10 &&
picked.id == 106 && strcmp(picked.name, "User6") == 0);
printf("{\n");
printf(" \"runtime\": \"GCC C11 (mstorsjo/llvm-mingw:20260616)\",\n");
printf(" \"cases\": [\n");
printf(" {\"name\": \"text_fprintf_fscanf_fgets\", \"expected\": \"This\", \"actual\": \"%s\", \"passed\": %s},\n",
w1, c1_pass ? "true" : "false");
printf(" {\"name\": \"binary_struct_and_eof\", \"expected\": \"101|Alice|95.5|eof=1\", \"actual\": \"%d|%s|%.1f|eof=%d\", \"passed\": %s},\n",
s_in.id, s_in.name, s_in.score, eof_after_short, c2_pass ? "true" : "false");
printf(" {\"name\": \"chunked_copy_and_alignment\", \"expected\": [20, 8, 5], \"actual\": [%zu, %zu, %zu], \"passed\": %s},\n",
copied, sz_mixed, sz_packed, c3_pass ? "true" : "false");
printf(" {\"name\": \"fseek_ftell_random_access\", \"expected\": \"10|106|User6\", \"actual\": \"%ld|%d|%s\", \"passed\": %s}\n",
record_count, picked.id, picked.name, c4_pass ? "true" : "false");
printf(" ]\n");
printf("}\n");
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。
入口文件:projects/c-file-io-lab/explore-file-io.c
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-file-io.c,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,GCC C11 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联63章) · 含当前章节或实例 · mbskycom202612a3210.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a3221.zip