1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h>
typedef struct { int year, month, day; int hour, minute, second; } DateTime;
typedef struct { int id; char name[100]; char email[100]; DateTime created_at; DateTime last_login; float balance; int transaction_count; bool is_active; } UserAccount;
typedef struct { char signature[8]; int version; int record_count; DateTime created_at; char description[64]; } FileHeader;
DateTime get_current_time(void) { time_t now = time(NULL); struct tm* tm_info = localtime(&now); DateTime dt = { .year = tm_info->tm_year + 1900, .month = tm_info->tm_mon + 1, .day = tm_info->tm_mday, .hour = tm_info->tm_hour, .minute = tm_info->tm_min, .second = tm_info->tm_sec }; return dt; }
bool save_user_accounts(const char* filename, UserAccount accounts[], int count) { FILE* file = fopen(filename, "wb"); if (file == NULL) { printf("无法创建文件: %s\n", filename); return false; } FileHeader header = { .signature = "USRDATA", .version = 1, .record_count = count, .created_at = get_current_time() }; strcpy(header.description, "User Account Database"); if (fwrite(&header, sizeof(FileHeader), 1, file) != 1) { printf("写入文件头失败\n"); fclose(file); return false; } if (fwrite(accounts, sizeof(UserAccount), count, file) != count) { printf("写入用户数据失败\n"); fclose(file); return false; } fclose(file); printf("成功保存 %d 个用户账户到 %s\n", count, filename); return true; }
UserAccount* load_user_accounts(const char* filename, int* count) { FILE* file = fopen(filename, "rb"); if (file == NULL) { printf("无法打开文件: %s\n", filename); return NULL; } FileHeader header; if (fread(&header, sizeof(FileHeader), 1, file) != 1) { printf("读取文件头失败\n"); fclose(file); return NULL; } if (strcmp(header.signature, "USRDATA") != 0) { printf("无效的文件格式\n"); fclose(file); return NULL; } printf("文件信息:\n"); printf(" 签名: %s\n", header.signature); printf(" 版本: %d\n", header.version); printf(" 记录数: %d\n", header.record_count); printf(" 创建时间: %d-%02d-%02d %02d:%02d:%02d\n", header.created_at.year, header.created_at.month, header.created_at.day, header.created_at.hour, header.created_at.minute, header.created_at.second); printf(" 描述: %s\n", header.description); UserAccount* accounts = malloc(header.record_count * sizeof(UserAccount)); if (accounts == NULL) { printf("内存分配失败\n"); fclose(file); return NULL; } if (fread(accounts, sizeof(UserAccount), header.record_count, file) != header.record_count) { printf("读取用户数据失败\n"); free(accounts); fclose(file); return NULL; } fclose(file); *count = header.record_count; printf("成功加载 %d 个用户账户\n", *count); return accounts; }
void print_user_account(const UserAccount* account) { printf("用户账户:\n"); printf(" ID: %d\n", account->id); printf(" 姓名: %s\n", account->name); printf(" 邮箱: %s\n", account->email); printf(" 余额: %.2f\n", account->balance); printf(" 交易次数: %d\n", account->transaction_count); printf(" 状态: %s\n", account->is_active ? "活跃" : "非活跃"); printf(" 创建时间: %d-%02d-%02d %02d:%02d:%02d\n", account->created_at.year, account->created_at.month, account->created_at.day, account->created_at.hour, account->created_at.minute, account->created_at.second); printf(" 最后登录: %d-%02d-%02d %02d:%02d:%02d\n", account->last_login.year, account->last_login.month, account->last_login.day, account->last_login.hour, account->last_login.minute, account->last_login.second); printf("\n"); }
void demonstrate_binary_serialization(void) { printf("=== 二进制序列化演示 ===\n"); UserAccount accounts[] = { { .id = 1001, .name = "张三", .email = "zhangsan@example.com", .created_at = {2023, 1, 15, 10, 30, 0}, .last_login = {2024, 8, 10, 14, 25, 30}, .balance = 1250.75f, .transaction_count = 45, .is_active = true }, { .id = 1002, .name = "李四", .email = "lisi@example.com", .created_at = {2023, 3, 22, 16, 45, 0}, .last_login = {2024, 8, 9, 9, 15, 20}, .balance = 890.50f, .transaction_count = 23, .is_active = true }, { .id = 1003, .name = "王五", .email = "wangwu@example.com", .created_at = {2023, 6, 8, 11, 20, 0}, .last_login = {2024, 7, 28, 20, 10, 45}, .balance = 0.00f, .transaction_count = 12, .is_active = false } }; int account_count = sizeof(accounts) / sizeof(accounts[0]); const char* filename = "user_accounts.dat"; if (save_user_accounts(filename, accounts, account_count)) { int loaded_count; UserAccount* loaded_accounts = load_user_accounts(filename, &loaded_count); if (loaded_accounts != NULL) { printf("\n加载的用户账户:\n"); for (int i = 0; i < loaded_count; i++) { printf("--- 用户 %d ---\n", i + 1); print_user_account(&loaded_accounts[i]); } free(loaded_accounts); } } remove(filename); }
int main(void) { demonstrate_binary_serialization(); return 0; }
|