C语言 15.结构体和文件

1.结构体嵌套二级指针练习

在这里插入图片描述

struct Teacher
{
    char* name;
    char** students;
};
void allocateSpace(struct Teacher*** teachers)
{
    if (teachers == NULL)
    {
        return;
    }
    //开辟内存
    struct Teacher** ts = malloc(sizeof(struct Teacher*) * 3);
    //给每个老师分配内存
    for (int i = 0; i < 3; i++)
    {
        ts[i] = malloc(sizeof(struct Teacher));
        //给老师的姓名分配内存
        ts[i]->name = malloc(sizeof(char) * 64);
        //给老师起名称
        sprintf(ts[i]->name, "Teacher_%d", i + 1);
        //给学生的数组分配内存
        ts[i]->students = malloc(sizeof(char*) * 4);
        //给学生的姓名开辟内存 以及赋值
        for (int j = 0; j < 4; j++)
        {
            ts[i]->students[j] = malloc(sizeof(char) * 64);
            sprintf(ts[i]->students[j], "%s_Student_%d", ts[i]->name, j + 1);
        }
    }
    *teachers = ts;
}
void printTeachers(struct Teacher** pArray)
{
    if (pArray == NULL)
    {
        return;
    }
    for (int i = 0; i < 3; i++)
    {
        printf("%s\n", pArray[i]->name);
        for (int j = 0; j < 4; j++)
        {
            printf("      %s\n", pArray[i]->students[j]);
        }
    }
}
void freeSpace(struct Teacher** pArray)
{
    if (pArray == NULL)
    {
        return;
    }
    for (int i = 0; i < 3; i++)
    {
        //先释放老师姓名
        if (pArray[i]->name != NULL)
        {
            free(pArray[i]->name);
            pArray[i]->name = NULL;
        }
        //释放学生姓名
        for (int j = 0; j < 4; j++)
        {
            if (pArray[i]->students[j] != NULL)
            {
                free(pArray[i]->students[j]);
                pArray[i]->students[j] = NULL;
            }
        }
        //释放学生的数组
        if (pArray[i]->students != NULL)
        {
            free(pArray[i]->students);
            pArray[i]->students = NULL;
        }
        //释放老师
        if (pArray[i] != NULL)
        {
            free(pArray[i]);
            pArray[i] = NULL;
        }
    }
    //释放老师数组
    if (pArray != NULL)
    {
        free(pArray);
        pArray = NULL;
    }
}
void test01()
{
    struct Teacher** pArray = NULL;
    //开辟内存
    allocateSpace(&pArray);
    //打印数组
    printTeachers(pArray);
    //释放数组
    freeSpace(pArray);
    pArray = NULL;

2.结构体偏移量

2.1获取属性偏移

2.1.1offsetof

2.1.2(int)&(p->b) - (int)p

2.2通过偏移量 获取内存

2.3结构体嵌套结构体


struct Teacher
{
    char a; //0 ~ 3
    int b;  //4 ~ 7
};
void test01()
{
    printf("b的属性偏移量为:%d\n", offsetof(struct Teacher, b));   // 4
    /*struct Teacher t1;
    struct Teacher* p = &t1;
    printf("b的属性偏移量为:%d\n", (int)&(p->b) - (int)p);
    printf("b的属性偏移量为:%d\n", offsetof(struct Teacher, b));*/
}
//通过偏移量 操作内存
void test02()
{
    struct Teacher t1 = { 'a', 10 };
    printf("t1.b = %d\n", *(int*)((char*)&t1 + offsetof(struct Teacher, b)));
    printf("t1.b = %d\n", *(int*)((int*)&t1 + 1));
}
struct Teacher2
{
    char a;
    int b;
    struct Teacher c;
};
void test03()
{
    struct Teacher2 t1 = { 'a', 10, 'b', 20 };
    int offset1 = offsetof(struct Teacher2, c);
    int offset2 = offsetof(struct Teacher, b);
    printf("%d\n", *(int*)((char*)&t1 + offset1 + offset2));
    printf("%d\n", ((struct Teacher*)((char*)&t1 + offset1))->b);
}
struct Teacher2
{
    char a;
    int b;
    struct Teacher c;  //展开就可以,相当于
    //char a; //0 ~ 3
    //int b;  //4 ~ 7
};
void test03()
{
    struct Teacher2 t1 = { 'a', 10, 'b', 20 };
    // 用二次偏移来找到Teacher中的b的偏移量
    int offset1 = offsetof(struct Teacher2, c);
    int offset2 = offsetof(struct Teacher, b);
    printf("%d\n", *(int*)((char*)&t1 + offset1 + offset2));
    /*
    &t1:首地址
    (char*)&t1 : 转成char
    ((char*)&t1 + offset1 + offset2):加上两次偏移
    *(int*)((char*)&t1 + offset1 + offset2):解数据    
    */
    printf("%d\n", ((struct Teacher*)((char*)&t1 + offset1))->b);
    /*
    ((char*)&t1 + offset1)) : 跳转到c
    (struct Teacher*)((char*)&t1 + offset1)) : 把步长改成Teacher类型的指针
    (struct Teacher*)((char*)&t1 + offset1))->b : 访问其中的b
    */
}

3.内存对齐

3.1查看对齐模数 #pragma pack(show)

3.2默认对齐模数 8

3.3自定义数据类型 对齐规则

3.3.1 第一个属性开始 从0开始偏移

3.3.2 第二个属性开始 要放在 该类型的大小 与 对齐模数比 取小的值 的整数倍

3.3.3 所有属性都计算完后,再整体做二次偏移,将整体计算的结果 要放在 结构体最大类型 与对齐模数比 取小的值的 整数倍上

3.4 结构体嵌套结构体

3.4.1结构体嵌套结构体时候,子结构体放在该结构体中最大类型和对齐模数比 的整数倍上即可
在这里插入图片描述


#pragma pack(show) //查看当前对齐模数 ,对齐模数是可以改的,改成2的N次方



//第一个属性开始  从0开始偏移

//第二个属性开始  要放在 该类型的大小  与  对齐模数比  取小的值  的整数倍

//所有属性都计算完后,再整体做二次偏移,将整体计算的结果 要放在  结构体最大类型 与对齐模数比  取小的值的 整数倍上



typedef struct _STUDENT{



    int a;    //0 ~ 3      0 ~ 3

    char b;   //4 ~ 7      4

    double c; //8 ~ 15     5 ~ 12

    float d;  //16 ~ 19    13 ~ 16

}Student;





void test01()

{

    printf("sizeof  student  = %d\n", sizeof(Student));



}



//结构体嵌套结构体时候,子结构体放在该结构体中最大类型 和对齐模数比 的整数倍上即可

typedef struct _STUDENT2{

    char a;  // 0 ~ 7

    Student b; // 8  ~ 31

    double c;  //32 ~ 39

}Student2;



void test02()

{

    printf("sizeof  student  = %d\n", sizeof(Student2));

}



4.文件读写回顾

4.1按照字符读写

4.1.1写 fputc

4.1.2读 fgetc

4.1.3while ( (ch = fgetc(f_read)) != EOF ) 判断是否到文件尾

4.2按行读写

4.2.1写 fputs

4.2.2读 fgets

4.3按块读写

4.3.1写 fwrite

4.3.1.1参数1 数据地址 参数2块大小 参数3块个数 参数4文件指针

4.3.2读 fread

4.4格式化读写

4.4.1写 fprintf

4.4.2读 fscanf

4.5随机位置读写

4.5.1fseek( 文件指针, 偏移, 起始位置 )

4.5.1.1SEEK_SET 从头开始

4.5.1.2SEEK_END 从尾开始

4.5.1.3SEEK_CUR 从当前位置

4.5.2rewind 将文件光标置首

4.6error宏 利用perror打印错误提示信息
在这里插入图片描述

/按照字符读写文件:fgetc(), fputc()

void test01()
{
    //写文件

    FILE * f_write = fopen("./test01.txt", "w+");

    if (f_write == NULL)
    {
        return;
    }

    char buf[] = "this is first test";
    for (int i = 0; i < strlen(buf);i++)
    {
        fputc(buf[i], f_write);
    }

    fclose(f_write);

    //读文件
    FILE * f_read = fopen("./test01.txt", "r");
    if (f_read == NULL)
    {
        return;
    }
    char ch;
    while ( (ch = fgetc(f_read)) != EOF  )  // EOF  Enf of File
    {
        printf("%c", ch);
    }
    fclose(f_read);
}


//按照行读写文件:fputs(), fgets()
void test02()
{
    //写文件
    FILE * f_write = fopen("./test02.txt", "w");
    if (f_write == NULL)
    {
        return;
    }
    char * buf[] =
    {
        "锄禾日当午\n",
        "汗滴禾下土\n",
        "谁知盘中餐\n",
        "粒粒皆辛苦\n",
    };

    for (int i = 0; i < 4;i++)
    {
        fputs(buf[i], f_write);
    }

    fclose(f_write);

    //读文件
    FILE * f_read = fopen("./test02.txt", "r");
    if (f_read == NULL)
    {
        return;
    }
    while ( !feof(f_read ))
    {
        char buf[1024] = { 0 };
        fgets(buf, 1024, f_read);
        printf("%s", buf);
    }

    fclose(f_read);
}

//按照块读写文件:fread(), fwirte()
struct Hero
{
    char name[64];
    int age;
};
void test03()
{
    //写文件  wb二进制方式
    FILE * f_write = fopen("./test03.txt", "wb");
    if (f_write == NULL)
    {
        return;
    }

    struct Hero heros[4] = 
    {
        { "亚瑟" , 18 },
        { "赵云", 28 },
        { "妲己", 19 },
        { "孙悟空", 99 },
    };

    for (int i = 0; i < 4;i++)
    {
        //参数1 数据地址  参数2  块大小   参数3  块个数   参数4  文件指针
        fwrite(&heros[i], sizeof(struct Hero), 1, f_write);
    }

    fclose(f_write);

    //读文件  
    FILE * f_read = fopen("./test03.txt", "rb");  // read binary
    if (f_read == NULL)
    {
        return;
    }

    struct Hero temp[4];
    //参数1 数据地址  参数2  块大小   参数3  块个数   参数4  文件指针
    fread(&temp, sizeof(struct Hero), 4, f_read);

    for (int i = 0; i < 4;i++)
    {
        printf("姓名:%s  年龄:%d \n", temp[i].name, temp[i].age);
    }
    fclose(f_read);
}

//按照格式化读写文件:fprintf(), fscanf() 遇到空格相当于换行操作
void test04()
{
    //写文件
    FILE * f_write = fopen("./test04.txt", "w");
    if (f_write == NULL)
    {
        return;
    }

    fprintf(f_write, "hello world %d年 %d月 %d日", 2018, 7, 5);

    //关闭文件
    fclose(f_write);


    //读文件
    FILE * f_read = fopen("./test04.txt", "r");
    if (f_read == NULL)
    {
        return;
    }

    char buf[1024] = { 0 };
    while (!feof(f_read))
    {
        fscanf(f_read, "%s", buf);
        printf("%s\n", buf);
    }

    fclose(f_read);
}

//按照随机位置读写文件
void test05()
{
    FILE * f_write = fopen("./test05.txt", "wb");
    if (f_write == NULL)
    {
        return;
    }

    struct Hero heros[4] =
    {
        { "亚瑟", 18 },
        { "赵云", 28 },
        { "妲己", 19 },
        { "孙悟空", 99 },
    };

    for (int i = 0; i < 4; i++)
    {
        //参数1 数据地址  参数2  块大小   参数3  块个数   参数4  文件指针
        fwrite(&heros[i], sizeof(struct Hero), 1, f_write);
    }

    fclose(f_write);


    //读取妲己数据
    FILE * f_read = fopen("./test05.txt", "rb");
    if (f_read == NULL)
    {
        //error 宏 
        
        //printf("文件打开失败\n");
        perror("文件打开失败");
        return;
    }

    //创建临时结构体
    struct Hero temp;

    //改变文件光标位置
    fseek(f_read, sizeof(struct Hero) *2, SEEK_SET);

    fseek(f_read, -(long)sizeof(struct Hero) * 2, SEEK_END);

    rewind(f_read); //将文件光标置首

    fread(&temp, sizeof(struct Hero), 1, f_read);

    printf("姓名: %s 年龄: %d\n", temp.name, temp.age);

    fclose(f_read);

}


5.文件读写注意事项

5.1注意事项1:

5.1.1不要用feof 按照字符方式读文件,原因有滞后性,会读出EOF

5.2注意事项2:

5.2.1如果属性开辟到堆区,不要存指针到文件中,要将指针指向的内容存放到文件中


//注意事项1

void test01()

{

    FILE * f_read = fopen("./test.txt", "r");

    if (f_read == NULL)

    {

        return;

    }



    char ch;



#if 0

    //aaaaaaaaaEOF

    //         |

    while ( !feof(f_read))

    {

        ch = fgetc(f_read);



        if (feof(f_read))

        {

            break;

        }



        printf("%c", ch);

    }

#endif 



    while ( ( ch = fgetc(f_read)) != EOF)

    {

        printf("%c", ch);

    }



    fclose(f_read);



}



//注意事项2 

struct Hero

{

    char * name; //如果属性开辟到堆区,不要存指针到文件中,要将指针指向的内容存放到文件中

    int age;

};

6.配置文件读写案例

6.1文件中按照键值对方式存放了有效的信息需要解析出来

6.2创建 config.h 和 config.c做配置文件读操作

6.3获取有效信息的行数 getFileLines

6.4判断字符串是否是有效行 int isValidLines(char *str)

6.5解析文件到配置信息数组中

6.5.1void parseFile(char * filePath, int lines , struct ConfigInfo ** configinfo);

6.6通过key获取value值

6.6.1char * getInfoByKey(char * key, struct ConfigInfo * configinfo, int len);

6.7释放内存

6.7.1void freeConfigInfo(struct ConfigInfo * configinfo);

需求:根据已有的config.txt文件来读取自己想要需要的内容,在这个案例里面即是 heroname:aaaa

config.txt


#英雄ID  注释

heroId:1

#英雄姓名  

heroName:琦玉

#英雄攻击力

heroAtk:9999999

#英雄防御力

heroDef:1000

#英雄简介

heroInfo:无敌







aaaaaaaaa

baflalsdf

strstr

struct ConfigInfo

{

   char key[64];

   char value[64];

}

//根据key值能够访问到对应的value

config.h


#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<string.h>

#include<stdlib.h>



//配置信息 结构体

struct ConfigInfo

{

    char key[64];

    char value[64];

};



//获取有效行数

int getFileLines(char * filePath);



//检测当前行是否是有效信息

int isValidLines(char *str);



//解析文件

void parseFile(char * filePath, int lines , struct  ConfigInfo ** configinfo);



//根据key获取对应value

char * getInfoByKey(char * key, struct ConfigInfo * configinfo, int len);



//释放内存

void freeConfigInfo(struct ConfigInfo * configinfo);

config.c


#include "config.h"



int getFileLines(char * filePath)

{

    FILE * file = fopen(filePath, "r");

    if (file == NULL)

    {

        return -1;

    }



    char buf[1024] = { 0 };

    int lines = 0;

    while ( fgets(buf,1024,file) != NULL)

    {

        if (isValidLines(buf))

        {

            lines++;

        }

        memset(buf, 0, 1024);

    }



    return lines;



    fclose(file);

}



int isValidLines(char *str)

{

    if (strchr(str, ':') == NULL)

    {

        return 0; //返回假 代表无效行

    }



    return 1;

}



//解析文件

void parseFile(char * filePath, int lines, struct  ConfigInfo ** configinfo)

{

    struct ConfigInfo * info = malloc(sizeof(struct ConfigInfo) * lines);



    if (info == NULL)

    {

        return;

    }



    FILE * file = fopen(filePath, "r");

    if (file == NULL)

    {

        return;

    }

    char buf[1024] = { 0 };

    int index = 0;

    while ( fgets(buf,1024,file ) != NULL)

    {

        if (isValidLines(buf))

        {

            //有效信息 才去解析

            //清空 key和value数组

            //heroName:aaaa\n

            memset(info[index].key, 0, 64);

            memset(info[index].value, 0, 64);

            char * pos = strchr(buf, ':');

            strncpy(info[index].key, buf, pos - buf);

            strncpy(info[index].value, pos + 1, strlen(pos + 1)-1);

            index++;

        }

        memset(buf, 0, 1024);

    }

    *configinfo = info;

}



char * getInfoByKey(char * key, struct ConfigInfo * configinfo, int len)

{

    for (int i = 0; i < len;i++)

    {

        if (strcmp(key, configinfo[i].key) == 0)

        {

            return configinfo[i].value;

        }

    }



    return NULL;

}



//释放内存

void freeConfigInfo(struct ConfigInfo * configinfo)

{

    if (configinfo != NULL)

    {

        free(configinfo);

        configinfo = NULL;

    }

}

main.c


#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include "config.h"





int main(){



    char * filePath = "./config.txt";

    int len = getFileLines(filePath);

    printf("文件的有效行数为:%d\n", len);



    struct ConfigInfo * configInfo = NULL;

    parseFile(filePath, len, &configInfo);



    //测试根据key获取value

    printf("heroId = %s\n", getInfoByKey("heroId", configInfo, len));

    printf("heroName = %s\n", getInfoByKey("heroName", configInfo, len));

    printf("heroAtk = %s\n", getInfoByKey("heroAtk", configInfo, len));

    printf("heroDef = %s\n", getInfoByKey("heroDef", configInfo, len));

    printf("heroInfo = %s\n", getInfoByKey("heroInfo", configInfo, len));



    //释放空间

    freeConfigInfo(configInfo);

    configInfo = NULL;





    system("pause");

    return EXIT_SUCCESS;

}

作业:加密数据

在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值