#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 = ; AdvTeacher * t = NULL; rv = CreateStructArray(&t, StructArrarySize, StudentNum); //被调函数分配内存
if (rv != )
{
printf("func: CreateStructArray() _%d_error_\n ", rv);
goto End;
} for (int i = ; i < StructArrarySize; ++i) // 客户端初始化赋值
{
printf("请输入第%d位老师的姓名: ", i+);
scanf("%s", t[i].name);
printf("请输入第%d位老师的年龄: ", i+);
scanf("%d", &(t[i].age));
printf("请输入第%d位老师的职务: ", i+);
scanf("%s", t[i].tile);
printf("请输入第%d位老师的地址: ", i+);
scanf("%s", t[i].addr);
for (int j = ; j < StudentNum; ++j)
{
printf("请输入第%d位老师的第%d位学生的姓名: ", i+, j+);
scanf("%s", t[i].student[j]);
}
} char * fileName = FileName; rv = StructWriteFile(fileName, t, StructArrarySize, StudentNum); // 结构体写入文件
if (rv != )
{
printf("func: StructWriteFile() _%d_error_\n ", rv);
goto End;
} ReadFileMenu(); //读取结构体文件 End:
rv = FreeStructArray(&t, StructArrarySize, StudentNum);
if (rv != )
{
printf("致命错误: FreeStructArray()执行失败!\n _%d_error_\n", rv);
}
system("pause");
return rv;
} // 创建结构体数组
int CreateStructArray(AdvTeacher **t, int structArrarySize, int studentNum)
{
int rv = ;
if (NULL == t)
{
rv = -;
return rv;
} AdvTeacher * temp = NULL; temp = (AdvTeacher *)malloc(structArrarySize * sizeof(AdvTeacher));
if (NULL == temp)
{
rv = -;
return rv;
} for (int i = ; i < structArrarySize; ++i)
{
temp[i].name = (char *)malloc( * sizeof(char));
temp[i].addr = (char *)malloc( * sizeof(char));
temp[i].tile = (char *)malloc( * sizeof(char)); if (NULL == temp[i].name || NULL ==temp[i].addr || NULL == temp[i].tile)
{
rv = -;
return rv;
} temp[i].student = (char **)malloc(studentNum * sizeof(char *));
if (NULL == temp[i].student)
{
rv = -;
return rv;
}
for (int j = ; j < studentNum; ++j) //创建学生内存块
{
(temp[i].student)[j] = (char *)malloc( * sizeof(char));
if (NULL == (temp->student)[j])
{
rv = -;
return rv;
}
}
} *t = temp; return rv;
} // 销毁结构体数组
int FreeStructArray(AdvTeacher **t, int structArrarySize, int studentNum)
{
int rv = ;
AdvTeacher *temp = *t; for (int i = ; i < structArrarySize; ++i)
{
for (int j = ; 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 = ;
if (NULL == fileName || NULL == t)
{
rv = -;
return rv;
} FILE *fp = NULL; fp = fopen(fileName, "w+");
if (NULL == fp)
{
printf("func: StructWriteFile() _文件打开失败_\n");
goto End;
} char buf[]; for (int i = ; i < structArrarySize; ++i)
{
sprintf(buf, "name%d = %s\n", i + , t[i].name);
fputs(buf, fp);
sprintf(buf, "age%d = %d\n", i + , t[i].age);
fputs(buf, fp);
sprintf(buf, "tile%d = %s\n", i + , t[i].tile);
fputs(buf, fp);
sprintf(buf, "addr%d = %s\n", i + , t[i].addr);
fputs(buf, fp); for (int j = ; j < studentNum; ++j)
{
sprintf(buf, "%dstudentname%d = %s\n",i+, j +, 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 = ;
FILE *fp = NULL;
char lineBuf[LineMaxLen];
char *pTmp = NULL, *pBegin = NULL, *pEnd = NULL; if (pFileName==NULL || pKey==NULL || pValue==NULL || pValueLen==NULL)
{
rv = -;
printf("StructFileRead() err. param err \n");
goto End;
} fp = fopen(pFileName, "r");
if (fp == NULL)
{
rv = -;
printf("fopen() err. \n");
goto End;
}
while (!feof(fp))
{
//读每一行
memset(lineBuf, , 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 + ; //获取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[];
char pValue[];
int pValueLen = ;
int rv = ; 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) < )
{
printf("\t非法输入!请重新输入\n");
system("pause");
continue;
}
if ( == strcmp("quit", pKey))
{
break;
} rv = StructFileRead(FileName, pKey, pValue, &pValueLen);
if ( != rv)
{
rv = -;
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_对结构体进行文件读写_张仕传_作业_的更多相关文章

  1. c++ 读写结构体到文件

    可以使用fwrite()将一个结构体写入文件:  fwrite(&some_struct,sizeof somestruct,1,fp);对应的fread函数可以再把它读出来,此处fwrite ...

  2. C语言中FILE是结构体,文件类型的指针

    c语言文件类型指针 我们在定义文件类型指针变量后,称作该指针指向该文件,但本质上,它不是指向一个存储文件信息的结构型变量么?那么我们在用各个函数对所谓的“文件指针”进行操作时,本质上是不是函数通过获取 ...

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

    struct file_operations{ struct module *owner; // 指向拥有该结构的模块的指针,避免正在操作时被卸载,一般为初始化为THIS_MODULES loff_t ...

  4. C语言博客作业--结构体,文件

    1.本章学习总结(2分) 1.1 学习内容总结 (1)结构体如何定义.成员如何赋值 结构体的一般形式为:      struct  结构体名     {      数据类型 成员名1:      数据 ...

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

    C语言学习宝典(5) 结构体: 将不同类型的数据组合成为一个有机的整体,这个整体就是一个结构体. 例如: Struct student { Int name; Char sex; Float scor ...

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

    /* 题目: //声明一个结构体类型 struct _AdvTeacher { char *name; char *tile; int age; char *addr; char *p1; //系统预 ...

  7. Delphi基础Write写入结构体到文件(使用 file of myrecord就行了,真简单)

    program WriteStruct; {$APPTYPE CONSOLE} uses SysUtils; //写入结构体 type TCustomer = record ID: ]; Code: ...

  8. Linux C Socket编程发送结构体、文件详解及实例

    利用Socket发送文件.结构体.数字等,是在Socket编程中经常需要用到的.由于Socket只能发送字符串,所以可以使用发送字符串的方式发送文件.结构体.数字等等. 本文:http://www.c ...

  9. C语言提高 (5) 第五天 结构体,结构体对齐 文件

    1昨日回顾 2作业讲解 3 结构体的基本定义 //1 struct teacher { int id; char name[64]; }; struct teacher t5 = { 5, " ...

随机推荐

  1. OO ALV 学习参考

      http://blog.csdn.net/sapliumeng/article/details/18653491 一.ALV介绍 The ALV Grid Control (ALV = SAPLi ...

  2. java 实现视频转换通用工具类:视频截图-Ffmpeg(四)

    java 实现视频转换通用工具类:获取视频元数据信息(一) java 实现视频转换通用工具类:视频相互转换-总方法及Mencoder(二) java 实现视频转换通用工具类:视频相互转换-Ffmpeg ...

  3. 【iOS知识学习】_iOS动态改变TableView Cell高度

    在做tableView的时候,我们有时候须要依据cell的高度动态来调整.近期在网上看到一段代码不错.跟大家Share一下. 在 -(UITableViewCell *)tableView:(UITa ...

  4. 滑雪_poj_1088(记忆化搜索).java

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 67987   Accepted: 25013 Description ...

  5. Effective C++ 条款45

    本节条款的题目是运用成员模板接受全部兼容类型 作者阐述自己的观点是通过智能指针的样例. 在学习本节条款之前我们要先明确关于隐式转化的问题 例如以下代码: #include<iostream> ...

  6. flash builder 4.7 debug via usb device iPhone 4s - device not found

    http://forums.adobe.com/message/4865192 Please provide more info on the above issue: 1.What is the m ...

  7. innobackupex --slave-info参数的含义和适用场景

    http://blog.itpub.net/28916011/viewspace-1969135/       我有个问题一直没弄明白,就是innobackupex里面的--slave-info这个参 ...

  8. Git链接到自己的Github(1)简单的开始

    好长时间没上来弄东西了,今天回来先开始弄下Git,之后再继续写uboot与kernel的编译,在版本控制下更加宏观地观察每次的变化. 1.在ubuntu中安装git $ sudo apt-get in ...

  9. iOS 符号表恢复 & 逆向支付宝

    推荐序 本文介绍了恢复符号表的技巧,并且利用该技巧实现了在 Xcode 中对目标程序下符号断点调试,该技巧可以显著地减少逆向分析时间.在文章的最后,作者以支付宝为例,展示出通过在 UIAlertVie ...

  10. Android 快速开发框架网络篇-Android-Async-Http

    一.基本用法 AsyncHttpClient client = new AsyncHttpClient(); client.get("http://www.google.com", ...