把相关参数进行配置时,很多需要写到文件里面保存
下面是一个代码,保存之。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
//灯数据结构体
struct _Lamp_Info
{
int number;
int color;
int size;
};
typedef struct _Lamp_Info Lamp_Info;
//灯数据节点
struct LampNode
{
Lamp_Info lamp_info;
struct LampNode *next;
};
//数据存储列表
struct ListData
{
int ListNumber;
struct LampNode *head;
struct LampNode *tail;
};
//数据存储列表定义
struct ListData List_Data_Table;
//数据存储列表初始化
int initListData()
{
List_Data_Table.ListNumber = 0;
List_Data_Table.head = NULL;
List_Data_Table.tail = NULL;
return 0;
}
//添加数据
int Add_List_info(struct LampNode *pLampNode)
{
if( pLampNode == NULL )
{
return -1;
}
if( List_Data_Table.head == NULL )
{
List_Data_Table.head = pLampNode;
List_Data_Table.tail = pLampNode;
pLampNode->next = NULL;
List_Data_Table.ListNumber++;
}
else
{
pLampNode->next = NULL;
List_Data_Table.tail->next = pLampNode;
List_Data_Table.tail = List_Data_Table.tail->next;
List_Data_Table.ListNumber++;
}
return 1;
}
//写入数据*****************************take care
int Save_Data_ToDisk(char *save_file_name)
{
int fd = 0;
int res= 0;
struct LampNode *p_tempNode = NULL;
fd = open(save_file_name, O_CREAT|O_RDWR);
if(fd < 0)
{
printf("Lamp.dat failed !\n");
return -1;
}
for( p_tempNode=List_Data_Table.head; p_tempNode!=NULL; p_tempNode=p_tempNode->next)
{
res = write(fd, &p_tempNode->lamp_info, sizeof(Lamp_Info));
if(res<0)
{
return -1;
}
}
}
//读出数据*************************
int Read_Data_FromDisk(char *read_file_name)
{
int fd = 0;
int res = 0;
Lamp_Info temp_lamp_info;
struct LampNode * p_tempNode = NULL;
fd = open(read_file_name,O_RDWR);
if(fd < 0)
{
printf("open Lamp.dat failed !\n");
return -1;
}
while(1)
{
memset((unsigned char*)&temp_lamp_info,0,sizeof(Lamp_Info));
res = read(fd, (unsigned char *)&temp_lamp_info, sizeof(Lamp_Info));
if( res < sizeof(Lamp_Info))
{
break;
}
p_tempNode = malloc(sizeof(struct LampNode));
memcpy((unsigned char *)&p_tempNode, (unsigned char *)&temp_lamp_info,sizeof(Lamp_Info));
//Add_List_info(p_tempNode);
printf("%d\n",temp_lamp_info.number);
}
}
//节点创造,准备添加
int CreateNode(Lamp_Info *plamp_info)
{
struct LampNode *tempNode = NULL;
tempNode = malloc(sizeof(struct LampNode));
memcpy((unsigned char*)&tempNode, (unsigned char*)&plamp_info, sizeof(Lamp_Info));
tempNode->next = NULL;
Add_List_info(tempNode);
}
//lamp数据获得,get_lamp_info
Lamp_Info get_lamp_info(int number, int color, int size)
{
Lamp_Info temp_lamp_info;printf("aaaaaa1\n");
memset((unsigned char*)&temp_lamp_info,0,sizeof(Lamp_Info));printf("aaaaaa2\n");
temp_lamp_info.number = number;printf("aaaaaa3\n");
temp_lamp_info.color = color;printf("aaaaaa4\n");
temp_lamp_info.size = size;printf("aaaaaa5\n");
return temp_lamp_info;
}
int main()
{
char save_name_org[] ="./Lamp.dat";
Lamp_Info temp_lamp_info = get_lamp_info(1,1,1);
printf("%d,%d,%d\n",temp_lamp_info.number,temp_lamp_info.color,temp_lamp_info.size);
CreateNode(&temp_lamp_info);printf("aaaaaa\n");
//CreateNode(get_lamp_info(2,2,2));
//CreateNode(get_lamp_info(3,3,3));
Save_Data_ToDisk(save_name_org);
Read_Data_FromDisk(save_name_org);
}