5.26续 C语言四、联合体与枚举4.1 联合体Unionc// 所有成员共享同一块内存typedef union {int i;float f;char str[20];} Data;int main() {Data d;printf(联合体大小%zu\n, sizeof(d)); // 20最大成员的大小d.i 100;printf(d.i %d\n, d.i); // 100printf(d.f %f\n, d.f); // 垃圾值d.f 3.14;printf(d.f %f\n, d.f); // 3.14printf(d.i %d\n, d.i); // 被破坏// 实用场景节省内存的类型识别return 0;}// 实用示例不同类型的值typedef struct {int type; // 0:int, 1:float, 2:stringunion {int int_val;float float_val;char *str_val;} value;} Variant;4.2 枚举Enumc// 枚举常量默认从0开始enum Color {RED, // 0GREEN, // 1BLUE // 2};// 自定义值enum Status {OK 200,NOT_FOUND 404,ERROR 500};// 使用typedeftypedef enum {MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY} Weekday;int main() {Weekday today WEDNESDAY;switch (today) {case MONDAY:case TUESDAY:case WEDNESDAY:case THURSDAY:case FRIDAY:printf(工作日\n);break;case SATURDAY:case SUNDAY:printf(周末\n);break;}return 0;}五、文件操作实战5.1 文件操作基本流程c// 1. 打开文件FILE *fp fopen(test.txt, r); // r:读, w:写, a:追加, rb:二进制读// 2. 检查是否成功if (fp NULL) {perror(文件打开失败); // 打印错误信息return 1;}// 3. 读写操作// ...// 4. 关闭文件fclose(fp);5.2 文本文件读写c#include stdio.h// 写文本文件void write_file() {FILE *fp fopen(students.txt, w);if (fp NULL) return;fprintf(fp, 姓名,年龄,成绩\n);fprintf(fp, 张三,20,85.5\n);fprintf(fp, 李四,21,92.0\n);fclose(fp);}// 读文本文件void read_file() {FILE *fp fopen(students.txt, r);if (fp NULL) return;char line[256];while (fgets(line, sizeof(line), fp) ! NULL) {printf(%s, line);}fclose(fp);}// 格式化读取void parse_file() {FILE *fp fopen(students.txt, r);if (fp NULL) return;char name[50];int age;float score;// 跳过第一行标题char header[256];fgets(header, sizeof(header), fp);while (fscanf(fp, %[^,],%d,%f\n, name, age, score) 3) {printf(姓名%s, 年龄%d, 成绩%.1f\n, name, age, score);}fclose(fp);}5.3 二进制文件读写ctypedef struct {int id;char name[50];float score;} Student;// 写二进制文件void write_binary(const char *filename, Student *students, int count) {FILE *fp fopen(filename, wb);if (fp NULL) return;fwrite(students, sizeof(Student), count, fp);fclose(fp);}// 读二进制文件void read_binary(const char *filename) {FILE *fp fopen(filename, rb);if (fp NULL) return;Student stu;while (fread(stu, sizeof(Student), 1, fp) 1) {printf(ID: %d, 姓名: %s, 成绩: %.1f\n,stu.id, stu.name, stu.score);}fclose(fp);}int main() {Student students[] {{1, 张三, 85.5},{2, 李四, 92.0},{3, 王五, 78.5}};write_binary(students.dat, students, 3);read_binary(students.dat);return 0;}5.4 文件定位随机访问c#include stdio.hvoid file_seek_example() {FILE *fp fopen(data.txt, r);if (fp NULL) return;// fseek(fp, offset, origin)// SEEK_SET: 文件开头, SEEK_CUR: 当前位置, SEEK_END: 文件结尾fseek(fp, 0, SEEK_END); // 移动到文件末尾long size ftell(fp); // 获取文件大小printf(文件大小%ld 字节\n, size);fseek(fp, 0, SEEK_SET); // 回到文件开头// 读取第100个字节fseek(fp, 100, SEEK_SET);char ch fgetc(fp);printf(第101个字符%c\n, ch);fclose(fp);}
Web渗透和杂项学习概况(第三周)5.27
发布时间:2026/5/28 6:25:12
5.26续 C语言四、联合体与枚举4.1 联合体Unionc// 所有成员共享同一块内存typedef union {int i;float f;char str[20];} Data;int main() {Data d;printf(联合体大小%zu\n, sizeof(d)); // 20最大成员的大小d.i 100;printf(d.i %d\n, d.i); // 100printf(d.f %f\n, d.f); // 垃圾值d.f 3.14;printf(d.f %f\n, d.f); // 3.14printf(d.i %d\n, d.i); // 被破坏// 实用场景节省内存的类型识别return 0;}// 实用示例不同类型的值typedef struct {int type; // 0:int, 1:float, 2:stringunion {int int_val;float float_val;char *str_val;} value;} Variant;4.2 枚举Enumc// 枚举常量默认从0开始enum Color {RED, // 0GREEN, // 1BLUE // 2};// 自定义值enum Status {OK 200,NOT_FOUND 404,ERROR 500};// 使用typedeftypedef enum {MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY} Weekday;int main() {Weekday today WEDNESDAY;switch (today) {case MONDAY:case TUESDAY:case WEDNESDAY:case THURSDAY:case FRIDAY:printf(工作日\n);break;case SATURDAY:case SUNDAY:printf(周末\n);break;}return 0;}五、文件操作实战5.1 文件操作基本流程c// 1. 打开文件FILE *fp fopen(test.txt, r); // r:读, w:写, a:追加, rb:二进制读// 2. 检查是否成功if (fp NULL) {perror(文件打开失败); // 打印错误信息return 1;}// 3. 读写操作// ...// 4. 关闭文件fclose(fp);5.2 文本文件读写c#include stdio.h// 写文本文件void write_file() {FILE *fp fopen(students.txt, w);if (fp NULL) return;fprintf(fp, 姓名,年龄,成绩\n);fprintf(fp, 张三,20,85.5\n);fprintf(fp, 李四,21,92.0\n);fclose(fp);}// 读文本文件void read_file() {FILE *fp fopen(students.txt, r);if (fp NULL) return;char line[256];while (fgets(line, sizeof(line), fp) ! NULL) {printf(%s, line);}fclose(fp);}// 格式化读取void parse_file() {FILE *fp fopen(students.txt, r);if (fp NULL) return;char name[50];int age;float score;// 跳过第一行标题char header[256];fgets(header, sizeof(header), fp);while (fscanf(fp, %[^,],%d,%f\n, name, age, score) 3) {printf(姓名%s, 年龄%d, 成绩%.1f\n, name, age, score);}fclose(fp);}5.3 二进制文件读写ctypedef struct {int id;char name[50];float score;} Student;// 写二进制文件void write_binary(const char *filename, Student *students, int count) {FILE *fp fopen(filename, wb);if (fp NULL) return;fwrite(students, sizeof(Student), count, fp);fclose(fp);}// 读二进制文件void read_binary(const char *filename) {FILE *fp fopen(filename, rb);if (fp NULL) return;Student stu;while (fread(stu, sizeof(Student), 1, fp) 1) {printf(ID: %d, 姓名: %s, 成绩: %.1f\n,stu.id, stu.name, stu.score);}fclose(fp);}int main() {Student students[] {{1, 张三, 85.5},{2, 李四, 92.0},{3, 王五, 78.5}};write_binary(students.dat, students, 3);read_binary(students.dat);return 0;}5.4 文件定位随机访问c#include stdio.hvoid file_seek_example() {FILE *fp fopen(data.txt, r);if (fp NULL) return;// fseek(fp, offset, origin)// SEEK_SET: 文件开头, SEEK_CUR: 当前位置, SEEK_END: 文件结尾fseek(fp, 0, SEEK_END); // 移动到文件末尾long size ftell(fp); // 获取文件大小printf(文件大小%ld 字节\n, size);fseek(fp, 0, SEEK_SET); // 回到文件开头// 读取第100个字节fseek(fp, 100, SEEK_SET);char ch fgetc(fp);printf(第101个字符%c\n, ch);fclose(fp);}