Loading... # 结构体,联合体与枚举类型 ## 结构体 int main() { //比如游戏Game中有,Name,HP,MP等数据很多变量,用结构体便可完美解决 struct GamePlayerInfo//游戏名 { char PlayerName[50]; int HP; int MP; float x; float y; float z; }; //系统设置玩家名和各项参数 struct GamePlayerInfo yitai = { "yitai",500,500,123.25,155.55,0 }; struct GamePlayerInfo weizhi = { .PlayerName = "weizhi",.MP = 1000 }; weizhi.x = 156.55; weizhi.y = 12231.121; weizhi.z = 0; weizhi.HP = 500; //也可以这样给用户输入获取设置 printf("设置你的游戏昵称:"); scanf("%s", weizhi.PlayerName); printf("%s", yitai.PlayerName);//打印游戏昵称 //设置NPC struct NPCInfo//游戏名 { char NPCName[50]; int HP; int MP; float x; float y; float z; }; struct NPCInfo MyNPCInfo[50];//批量化生产 for (size\_t i = 0; i < 50; i++) { strcpy(MyNPCInfo[i].NPCName, "NPC"); MyNPCInfo[i].HP = 100; MyNPCInfo[i].MP = 100; MyNPCInfo[i].HP = rand();//设置成随机数 } ## 结构体的嵌套结构。可以放在头文件里 struct NPCInfo//游戏名 { char NPCName[50]; int HP; int MP; float x; float y; float z; }; struct GamePlayerInfo//游戏名 { char PlayerName[50]; int HP; int MP; float x; float y; float z; struct NPCInfo MyNPCInfo; }; struct GamePlayerInfo yitai; yitai.MyNPCInfo.HP = 100; printf("%d", yitai.MyNPCInfo.HP); ## 指针操作结构体 struct NPCInfo//游戏名 { char NPCName[50]; int HP; int MP; float x; float y; float z; }; struct GamePlayerInfo//游戏名 { char PlayerName[50]; int HP; int MP; float x; float y; float z; struct NPCInfo MyNPCInfo; }; struct GamePlayerInfo yitai = { "yitai",100,100,45.111,1232.55,858.55 }; struct GamePlayerInfo \* p; p = &yitai; printf("%s", p->PlayerName);//两种方式都一样可以调用 printf("%s", (\*p).PlayerName);//两种方式都一样可以调用 ## 联合体学习笔记咯! union Info { char PlayerName[20]; int MP; float x; }; union Info MyInfo; strcpy(MyInfo.PlayerName, "NPC"); printf("%s\\n%d\\n%f\\n", MyInfo.PlayerName, MyInfo.MP, MyInfo.x); // 0x00D1FA1C = MyInfo = MyInfo.x = MyInfo.MP = My.playerName地址全都相同 //这说明不能同时使用连接体里面的多个元素,当用多个使用时只有最后一个有用,所以每次只能用一个 ## 枚举类型笔记咯!(算是整数型) enum color{red,blue,green};//这样是默认从零开始的 //enum color{red = 10,blue = 20,green = 30};也可以这样自定义 int flag = 0; printf("输入一个数:"); scanf("%d", &flag); switch (flag) { case red: printf("red"); break; case blue: printf("blue"); break; case green: printf("green"); break; default: break; } ## typedef的使用: 取别名 typedef struct GamePlayerInfo { char PlayerName[50]; int HP; int MP; float x; float y; float z; }yitai;//这样就是可以用yitai代替了struct GamePlayerInfo,变得更简洁 //给类型取别名 typedef int taiyi; taiyi m = 55; //typedef和#define的区别 //前者替换的是类型,后面的是替换的是值 return 0; } # 文件操作 文件的读取: 首先啊,要先创建一个文件并写入一段内容,并记一下文件路径,如图: 头文件: 源代码: //文件指针:位于文件最开始的位置上,可以来回移动到任意位置,从任意位置开始读取任意长度的数据,通过移动指针进行文件读取,移动到最后位置结束! //文件读取 [int]() main() { FILE \*pFile;//文件指针类型 char \* szReadTextBuffer;//缓冲区 int nReadFileSize;//用来读取文件长度 int nReadRetSize;//返回长度 pFile=fopen("C://xuexi/123456.txt", "rb"); //fopen()里面有两个参数("文件路径","标识符") //r读取 w写出 b二进制 if (pFile == NULL)//如果读取文件失败 { printf("Open filoe failed!"); exit(0);//退出程序 } fseek(pFile, 0, SEEK\_END);//把文件指针放到末尾处,通过文件指针位置,这样就可以获取文件长度 //fseek()里面有三个参数(文件指针,0,移动位置) nReadFileSize = ftell(pFile);//ftell(文件指针)通过文件指针位置,获取文件大小 rewind(pFile);//rewind(文件指针)把文件指针复位到最前面 //申请一个足够大的内存来保存我们读取进来的东西 szReadTextBuffer = (char \*)malloc((nReadFileSize \* sizeof(char)) + 1); if (szReadTextBuffer == NULL)//如果内存申请失败 { printf("malloc memory failed!"); exit(0); } memset(szReadTextBuffer, 0, nReadFileSize + 1);//给申请的内存初始化 //memest() 里面的参数有三个(要初始化的缓冲区,(要刷的值)0,要初始化的长度) nReadRetSize = fread(szReadTextBuffer, 1, nReadFileSize ,pFile);//文件读取 //fread() 里面有四个参数(可存储数据的内存空间,((要传的值)1,要读取的数据长度,文件指针) if (nReadRetSize != nReadFileSize)//如哦读取的真实长度和预设的不相等 { printf("read file failed!"); exit(0); } puts(szReadTextBuffer); fclose(pFile);//把文件指针释放掉 //printf("计算char类型数据宽度:%d\\n", sizeof(char)); return 0; } 运行结果: //写入文件 源代码: int main() { char \* szWriteBuffer = "My name is yitai! hahahahaha"; FILE \* pFile; pFile = fopen("C://xuexi/654321.txt", "wb"); if (pFile == NULL) { printf("failed!"); exit(0); } fwrite(szWriteBuffer, strlen(szWriteBuffer), 1, pFile); //写出函数,fwrite()里面有四个参数(要写出的东西,要写的长度,每次写出多少(1),文件指针) fclose(pFile);//怎么说也要报文件指针释放掉吧。 return 0; } 运行结果: 在指定位置之下创建文件并写入。 总给: 1. 这一次的内容,比较新鲜,应花更多的时间去记忆。 2. 内容较多,要多记忆和练习。 最后修改:2023 年 06 月 25 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 3 如果觉得我的文章对你有用,请随意赞赏