表示成这个形式
txt中文本
大体思路是这样,没有考虑容错
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define person_info_file "person_info.txt"
#define NAME_LENGTH 16
#define PROVINCE_LENGTH 16
#define DATE_LENGTH 32
#define SEX_LENGTH 8
#define EDUCATION_LEVEL_LENGTH 32
#define PERSON_COUNT 32
#define ONE_LINE_LENGTH 1024
#define CONTENT_STRING_LENGTH 32
typedef struct TreeNode
{
char name[NAME_LENGTH];
char province[PROVINCE_LENGTH];
char birthday[DATE_LENGTH];
char deathday[DATE_LENGTH];
char sex[SEX_LENGTH];
uint8_t height;
char education[EDUCATION_LEVEL_LENGTH];
uint8_t age;
} person_info_t;
//通过依次查找空格(' ')的位置,从而获取对应字段,填充person_info
void set_person_info(const char *str, person_info_t *person_info)
{
char content[CONTENT_STRING_LENGTH] = {0};
char *start = NULL;
char *end = NULL;
start = str;
end = strchr(start, ' ');
memcpy(content, start, end - start);
strcpy(person_info->name, content);
start = end + 1;
while (*start == ' ')
{
start++;
}
end = strchr(start, ' ');
memcpy(content, start, end - start);
strcpy(person_info->province, content);
start = end + 1;
while (*start == ' ')
{
start++;
}
end = strchr(start, ' ');
memcpy(content, start, end - start);
strcpy(person_info->birthday, content);
start = end + 1;
while (*start == ' ')
{
start++;
}
end = strchr(start, ' ');
memcpy(content, start, end - start);
strcpy(person_info->deathday, content);
start = end + 1;
while (*start == ' ')
{
start++;
}
end = strchr(start, ' ');
memcpy(content, start, end - start);
strcpy(person_info->sex, content);
start = end + 1;
while (*start == ' ')
{
start++;
}
end = strchr(start, ' ');
memcpy(content, start, end - start);
person_info->height = strtol(content, NULL, 10);
start = end + 1;
while (*start == ' ')
{
start++;
}
end = strchr(start, ' ');
memcpy(content, start, end - start);
strcpy(person_info->education, content);
start = end + 1;
while (*start == ' ')
{
start++;
}
end = strchr(start, ' ');
memcpy(content, start, end - start);
person_info->age = strtol(content, NULL, 10);
}
int main()
{
uint8_t i = 0;
person_info_t person_infos[PERSON_COUNT];
char buff[ONE_LINE_LENGTH] = {0};
int fp = fopen(person_info_file, "r");
while (fgets(buff, ONE_LINE_LENGTH - 1, fp) != NULL)
{
set_person_info(buff, &person_infos[i]);
i++;
}
return 0;
}