c语言 链表 malloc,C语言,for循环创建链表节点,第二次循环的到链表malloc就直接触发断点...

[Asm] 纯文本查看 复制代码#include"readwreit.h"

//获取有效行文件

int GetRow_Info(FILE *file)

{

if (NULL == file)

{

return 0;

}

int lines = 0;

char buf[1024] = { 0 };

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

{

//调用JudgRow_Info函数 判断当前行是否有效

if (!JudgRow_Info(buf))

{

continue;

}

memset(buf, 0, 1024);

++lines;

}

//返回有效行数,重置指针到起始位置

rewind(file);

return lines;

}

//加载配置文件

void Loading_Info(const char *filePath, char ***fileData, int *line)

{

if (NULL == filePath)

{

return;

}

if (NULL == fileData)

{

return;

}

if (NULL == line)

{

return;

}

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

//调用GetRow_Info函数获取有效行

int lines = GetRow_Info(file);

int index = 0;

char buf[1024] = { 0 };

char **temp = malloc(sizeof(char *)*lines);

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

{

//调用JudgRow_Info函数 判断当前行是否有效

if (!JudgRow_Info(buf))

{

continue;

}

temp[index] = malloc(sizeof(char *) * sizeof(buf) + 1);

strcpy(temp[index], buf);

++index;

memset(buf, 0, 1024);

}

//关闭文件,返回指存放有效数据的指针 有效行

fclose(file);

*fileData = temp;

*line = lines;

}

//解析文件

void Parse_Info(struct Info **info, char **fileData, int line)

{

if (NULL == fileData)

{

return;

}

if (NULL == info)

{

return;

}

int index = 0;

//创建头节点

struct Info *header = malloc(sizeof(struct Info));

memset(header, 0, sizeof(struct Info));

//定义一个辅助指针始终指向尾节点

struct Info *Rear = header;

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

{

//创建新节点

struct Info *newnoed = malloc(sizeof(struct Info));

memset(newnoed, 0, sizeof(struct Info));

//新节点数据域的两个指针分配对应大小空间

char *pos = strchr(fileData[index], ':');

newnoed->key = malloc(pos - fileData[index] + 1);

newnoed->val = malloc(strlen(pos + 1) + 1);

memset(newnoed->key, 0, pos - fileData[index] + 1);

memset(newnoed->val, 0, strlen(pos + 1) + 1);

//以指定格式把数据拷贝到链表中

strncpy(newnoed->key, fileData[index], pos - fileData[index]);

strncpy(newnoed->val, pos + 1, strlen(pos + 1) - 1);

newnoed->next = NULL;

//新节点添加到链表中,辅助指针指向最后一个节点

Rear->next = newnoed;

Rear = Rear->next;

++index;

}

//释放解析文件空间

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

{

free(fileData[i]);

fileData[i] = NULL;

}

free(fileData);

fileData = NULL;

*info = header;

}

//获取指定信息

char *Getinfo_Info(struct Info *info, char *dest)

{

if (NULL == info)

{

return 0;

}

if (NULL == dest)

{

return 0;

}

struct Info *pCurrent = info->next;

while (pCurrent != NULL)

{

if (strcmp(dest, pCurrent->key) == 0)

{

return pCurrent->val;

}

pCurrent = pCurrent->next;

}

return NULL;

}

//释放当前空间

void Destroy_Info(struct Info *info, int size)

{

struct Info *temp = info->next;

while (temp->next != NULL)

{

if (temp->key != NULL)

{

free(temp->key);

temp->key = NULL;

}

if (temp->val != NULL)

{

free(temp->val);

temp->val = NULL;

}

temp = temp->next;

}

}

//判断当前行是否有效

int JudgRow_Info(const char *buf)

{

if (buf[0] == '#' || buf[0] == '\n' || strchr(buf, ':') == NULL)

{

return 0;

}

return 1;

}

int main()

{

char **fileData = NULL;

int line = 0;

Loading_Info("config.txt",

&fileData, &line);

struct Info *info = NULL;

Parse_Info(&info, fileData, line);

printf("key:%s\n", Getinfo_Info(info, "password"));

system("pause");

Destroy_Info(info, line);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值