91行的后面加上
memset(newnoed->key, 0, pos - fileData[index] + 1);
memset(newnoed->val, 0, strlen(pos + 1) + 1);
行不行?
其他好像没啥问题呀!
[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;
}
|