Demo02_对结构体进行文件读写_张仕传_作业_


#include <iostream>

using namespace std;

#define StructArrarySize 5 // 老师数量
#define StudentNum 3 // 每位老师的学生的数量
#define FileName "f:/1.txt" // 文件路径和名称
#define LineMaxLen 1024 // 每行最大长读
#define KeyMaxLen 20 // key的最大长度

typedef struct _AdvTeacher
{
char *name;
char *tile;
int age;
char *addr;
char **student;
}AdvTeacher;

int CreateStructArray(AdvTeacher **, int, int); //客户端初始化结构体数组
int FreeStructArray(AdvTeacher **, int, int); //客户端释放结构体数组内存
int StructWriteFile(const char *, const AdvTeacher *, int, int); //结构体写入文件
int StructFileRead(char *pFileName, char *pKey, char *pValue, int *pValueLen); //读取结构体文件
void ReadFileMenu(void); //读取文件菜单

int main(void)
{
int rv = 0;

AdvTeacher * t = NULL;

rv = CreateStructArray(&t, StructArrarySize, StudentNum); //被调函数分配内存
if (rv != 0)
{
printf("func: CreateStructArray() _%d_error_\n ", rv);
goto End;
}

for (int i = 0; i < StructArrarySize; ++i) // 客户端初始化赋值
{
printf("请输入第%d位老师的姓名: ", i+1);
scanf("%s", t[i].name);
printf("请输入第%d位老师的年龄: ", i+1);
scanf("%d", &(t[i].age));
printf("请输入第%d位老师的职务: ", i+1);
scanf("%s", t[i].tile);
printf("请输入第%d位老师的地址: ", i+1);
scanf("%s", t[i].addr);
for (int j = 0; j < StudentNum; ++j)
{
printf("请输入第%d位老师的第%d位学生的姓名: ", i+1, j+1);
scanf("%s", t[i].student[j]);
}
}

char * fileName = FileName;

rv = StructWriteFile(fileName, t, StructArrarySize, StudentNum); // 结构体写入文件
if (rv != 0)
{
printf("func: StructWriteFile() _%d_error_\n ", rv);
goto End;
}

ReadFileMenu(); //读取结构体文件

End:
rv = FreeStructArray(&t, StructArrarySize, StudentNum);
if (rv != 0)
{
printf("致命错误: FreeStructArray()执行失败!\n _%d_error_\n", rv);
}
system("pause");
return rv;
}

// 创建结构体数组
int CreateStructArray(AdvTeacher **t, int structArrarySize, int studentNum)
{
int rv = 0;
if (NULL == t)
{
rv = -1;
return rv;
}

AdvTeacher * temp = NULL;

temp = (AdvTeacher *)malloc(structArrarySize * sizeof(AdvTeacher));
if (NULL == temp)
{
rv = -2;
return rv;
}

for (int i = 0; i < structArrarySize; ++i)
{
temp[i].name = (char *)malloc(256 * sizeof(char));
temp[i].addr = (char *)malloc(256 * sizeof(char));
temp[i].tile = (char *)malloc(256 * sizeof(char));

if (NULL == temp[i].name || NULL ==temp[i].addr || NULL == temp[i].tile)
{
rv = -3;
return rv;
}

temp[i].student = (char **)malloc(studentNum * sizeof(char *));
if (NULL == temp[i].student)
{
rv = -4;
return rv;
}
for (int j = 0; j < studentNum; ++j) //创建学生内存块
{
(temp[i].student)[j] = (char *)malloc(256 * sizeof(char));
if (NULL == (temp->student)[j])
{
rv = -5;
return rv;
}
}
}

*t = temp;

return rv;
}

// 销毁结构体数组
int FreeStructArray(AdvTeacher **t, int structArrarySize, int studentNum)
{
int rv = 0;
AdvTeacher *temp = *t;

for (int i = 0; i < structArrarySize; ++i)
{
for (int j = 0; j < studentNum; ++j) // 销毁学生内存块
{
if (NULL != temp[i].student[j])
{
free(temp[i].student[j]);
}
}

if (NULL != temp[i].addr && NULL != temp[i].name && NULL != temp[i].tile && NULL != temp[i].student)
{
free(temp[i].addr);
free(temp[i].name);
free(temp[i].tile);
free(temp[i].student);
}
}

if (NULL != temp)
{
free(temp);
*t = NULL; //间接赋值 通过*(实参的地址), 去间接修改实参的值 为null
}

return rv;
}

// 结构体文件写入
int StructWriteFile(const char *fileName, const AdvTeacher *t, int structArrarySize, int studentNum)
{
int rv = 0;
if (NULL == fileName || NULL == t)
{
rv = -1;
return rv;
}

FILE *fp = NULL;

fp = fopen(fileName, "w+");
if (NULL == fp)
{
printf("func: StructWriteFile() _文件打开失败_\n");
goto End;
}

char buf[1024];

for (int i = 0; i < structArrarySize; ++i)
{
sprintf(buf, "name%d = %s\n", i + 1, t[i].name);
fputs(buf, fp);
sprintf(buf, "age%d = %d\n", i + 1, t[i].age);
fputs(buf, fp);
sprintf(buf, "tile%d = %s\n", i + 1, t[i].tile);
fputs(buf, fp);
sprintf(buf, "addr%d = %s\n", i + 1, t[i].addr);
fputs(buf, fp);

for (int j = 0; j < studentNum; ++j)
{
sprintf(buf, "%dstudentname%d = %s\n",i+1, j +1, t[i].student[j]); //第几个老师的第几个学生
fputs(buf, fp);
}
}

End:
if (NULL != fp)
{
fclose(fp);
}
return rv;
}

// 结构体文件读出
int StructFileRead(char *pFileName, char *pKey, char *pValue, int *pValueLen)
{
int rv = 0;
FILE *fp = NULL;
char lineBuf[LineMaxLen];
char *pTmp = NULL, *pBegin = NULL, *pEnd = NULL;

if (pFileName==NULL || pKey==NULL || pValue==NULL || pValueLen==NULL)
{
rv = -1;
printf("StructFileRead() err. param err \n");
goto End;
}

fp = fopen(pFileName, "r");
if (fp == NULL)
{
rv = -2;
printf("fopen() err. \n");
goto End;
}
while (!feof(fp))
{
//读每一行
memset(lineBuf, 0, sizeof(lineBuf));
pTmp = fgets(lineBuf, LineMaxLen, fp);
if (pTmp == NULL)
{
break;
}

//不含=, 非配置项
pTmp = strchr(lineBuf, ‘=‘);
if (pTmp == NULL)
{
continue;
}
//key是否在本行
pTmp = strstr(lineBuf, pKey);
if (pTmp == NULL)
{
continue;
}

//调整到=右边,取value准备
pTmp = strchr(lineBuf, ‘=‘);
if (pTmp == NULL)
{
continue;
}
pTmp = pTmp + 1;

//获取value 起点
while (true)
{
if (*pTmp == ‘ ‘)
{
pTmp ++ ;
}
else
{
pBegin = pTmp;
if (*pBegin == ‘\n‘)
{
//没有配置value
printf("配置项:%s 没有配置value \n", pKey);
goto End;
}
break;
}
}

//获取valude结束点
while (true)
{
if ((*pTmp == ‘ ‘ || *pTmp == ‘\n‘))
{
break;
}
else
{
pTmp ++;
}
}
pEnd = pTmp;

//赋值
*pValueLen = pEnd-pBegin;
memcpy(pValue, pBegin, pEnd-pBegin);
pValue[pEnd - pBegin] = ‘\0‘;
break;
}

End:
if (fp != NULL)
{
fclose(fp);
}

return rv;
}

// 文件读出菜单
void ReadFileMenu(void)
{
char pKey[256];
char pValue[1024];
int pValueLen = 0;
int rv = 0;

while (true)
{
strcpy(pValue, "不存在匹配项");
system("cls");
printf("\t********************读取文件菜单************************\n");
printf("\t格式:如第一个老师的姓名: name1\n");
printf("\t格式:如第一个老师的第二个学生的姓名: 1studentname2\n");
printf("\t退出请输入: quit \n\n");
printf("\t********************读取文件菜单************************\n");
printf("\t\n");
printf("\t请输入需要读取数据: ");

scanf("%s", pKey);
if (strlen(pKey) < 4)
{
printf("\t非法输入!请重新输入\n");
system("pause");
continue;
}
if (0 == strcmp("quit", pKey))
{
break;
}

rv = StructFileRead(FileName, pKey, pValue, &pValueLen);
if (0 != rv)
{
rv = -1;
printf("func: StructFileRead() _%d_error_\n", rv);
return;
}

printf("\n\t%s = %s\n\n", pKey, pValue);
printf("\t请按任意键继续!\n");
strcpy(pValue, "不存在匹配项"); //覆盖上一次pValue的值,这样就可以避免下次如果没有找到pValue,这次的值不会残留到下一个pValue
system("pause");

}

return;
}

Demo02_对结构体进行文件读写_张仕传_作业_,布布扣,bubuko.com

时间: 2024-08-10 21:20:26

Demo02_对结构体进行文件读写_张仕传_作业_的相关文章

Demo_张仕传_结构体考试-modify

/* 题目: //声明一个结构体类型 struct _AdvTeacher { char *name; char *tile; int age; char *addr; char *p1; //系统预留成员域 char **p2;//系统预留成员域 }; 要求定义一个结构体数组(6个元素),要求从键盘输入数据,并按照名称大小进行排序:打印输出. 1. 打印结构体数组,需要单独封装成函数:10 2. 排序结构体数组,需要单独封装成函数(按照名称进行排序):50 3. main函数中编写业务测试模型

保存结构体到文件

经常有这样一种需求,希望有些设置的信息(比如说账号信息)能够掉电后不丢失,重新开机后能够重新读出来.最简单的做法是把信息保存在文件中,文件在nand flash上就不会掉电丢失. 我们不仅可以向文件中写字符串,其实写结构体也是可以的.注意结构体里面不能有指针. 假设我们要保存一个账号结构体到文件, #include <stdio.h>#include <errno.h>#include <string.h>typedef struct _Account{ char us

几年前做家教写的C教程(之五专讲结构体与文件操作)

C语言学习宝典(5) 结构体: 将不同类型的数据组合成为一个有机的整体,这个整体就是一个结构体. 例如: Struct student { Int name; Char sex; Float score; }: 使用方法: 类型名 成员名: 一般形式: Struct { 成员列表: }变量名表列: 结构体变量的引用: 结构体变量名.成员名 文件: FILE  *fp; Fp=fopen(文件名,文件打开方式): Fclose(文件指针) 例1  对候选人得票的统计程序,设有3个后选人,每次输入一

cdev成员结构体file_operations文件操作结构的分析

struct file_operations{ struct module *owner; // 指向拥有该结构的模块的指针,避免正在操作时被卸载,一般为初始化为THIS_MODULES loff_t (*llseek) (struct file *, loff_t, int); // llseek用来修改文件当前的读写位置,返回新位置 // loff_t为一个”长偏移量”.当此函数指针为空,seek调用将会以不可预期的方式修改file结构中的位置计数器. ssize_t (*read) (st

用结构体读取文件

#include<stdio.h>#include<stdlib.h>#include<string.h> int main(void){ struct data { char id[20]; char name[20]; char sex[10]; int old; char number[20]; char habit[10]; char b[10]; int piao; } student[10]; FILE *fp; if((fp=fopen("shi

实验13——结构体、文件的基本应用

1. 本次课学习到的知识点: (1)结构类型是一种允许把一些数据分量聚合成一个整体的数据类型. (2)结构与数组的区别在于:数组中所有元素的数据类型必须是相同的,而结构中各成员的数据类型可以不同. (3) struct是定义结构类型的关键字,在struct之后,自行 命名一个结构名,它必须是一个合法的标识符. struct 结构名{ 类型名 结构成员名1; 类型名 结构成员名2; ··· 类姓名 结构成员名n; }; (4)在定义嵌套的结构类型时,必须定义成员的结构类型,再 定义主结构类型. (

结构体嵌套,指针形参,引用传参

1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 6 struct Student 7 { 8 int s_id; 9 string s_name; 10 int s_phonenum; 11 }; 12 13 struct Teacher 14 { 15 int m_id; 16 string m_name; 17 Student m_stu;//班长 18 Student starr[50];/

C7_多结构体文件

.h // //  MyFunction.h //  C7_结构体多文件 // //  Created by dllo on 15/7/8. //  Copyright (c) 2015年 zhozhicheng. All rights reserved. // #import <Foundation/Foundation.h> // .h文件, 我们写函数的声明 // .m文件,写函数的定义 // 声明一个学生类型的结构体,用typedef设置 struct student{ int stu

golang笔记(1)-数据库查询结果映射至结构体

通用的映射模式 query:="select id,name from user where id=?" //单个结构体ret:=&Activity{} DbClient().Find(query,activityId).Unique(ret)//结构体数组ret:=[]Activity{} DbClient().Find(query,activityId).List(&ret)   1.定义结构体 type Activity struct{ ID int64 `col