zoukankan      html  css  js  c++  java
  • [学习记录]接口的封装与设计之文件读写

    #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_
     ", 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_
     ", rv);
            goto End;
        }
    
        ReadFileMenu();                                                      //读取结构体文件
    
    End:
        rv = FreeStructArray(&t, StructArrarySize, StudentNum);
        if (rv != 0)
        {
            printf("致命错误: FreeStructArray()执行失败!
     _%d_error_
    ", 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() _文件打开失败_
    ");
            goto End;
        }
    
        char buf[1024];
    
        for (int i = 0; i < structArrarySize; ++i)
        {
            sprintf(buf, "name%d = %s
    ", i + 1, t[i].name);
            fputs(buf, fp);
            sprintf(buf, "age%d  = %d
    ", i + 1, t[i].age);
            fputs(buf, fp);
            sprintf(buf, "tile%d = %s
    ", i + 1, t[i].tile);
            fputs(buf, fp);
            sprintf(buf, "addr%d = %s
    ", i + 1, t[i].addr);
            fputs(buf, fp);
    
            for (int j = 0; j < studentNum; ++j)
            {
                sprintf(buf, "%dstudentname%d = %s
    ",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 
    ");
            goto End;
        }
    
        fp = fopen(pFileName, "r");
        if (fp == NULL)
        {
            rv = -2;
            printf("fopen() err. 
    ");
            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 == '
    ')
                    {
                        //没有配置value
                        printf("配置项:%s 没有配置value 
    ", pKey);
                        goto End;
                    }
                    break;
                }
            }
    
            //获取valude结束点
            while (true) 
            {
                if ((*pTmp == ' ' || *pTmp == '
    '))
                {
                    break;
                }
                else 
                {
                    pTmp ++;
                }
            }
            pEnd = pTmp;
    
            //赋值
            *pValueLen = pEnd-pBegin;
            memcpy(pValue, pBegin, pEnd-pBegin);
            pValue[pEnd - pBegin] = '';
            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("	********************读取文件菜单************************
    ");
            printf("	格式:如第一个老师的姓名: name1
    ");
            printf("	格式:如第一个老师的第二个学生的姓名: 1studentname2
    ");
            printf("	退出请输入: quit 
    
    ");
            printf("	********************读取文件菜单************************
    ");
            printf("	
    ");
            printf("	请输入需要读取数据: ");
    
            scanf("%s", pKey);
            if (strlen(pKey) < 4)
            {
                printf("	非法输入!请重新输入
    ");
                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_
    ", rv);
                return;
            }
        
            printf("
    	%s = %s
    
    ", pKey, pValue);
            printf("	请按任意键继续!
    ");
            strcpy(pValue, "不存在匹配项");               //覆盖上一次pValue的值,这样就可以避免下次如果没有找到pValue,这次的值不会残留到下一个pValue
            system("pause");
            
        }
    
        return;
    }
  • 相关阅读:
    windows下端口映射(端口转发)
    SQLServer 2008 复制同步(发布、订阅)的几个问题
    SqlServer:此数据库处于单用户模式,导致数据库无法删除的处理
    jQuery函数的等价原生函数【转载】
    JavaScript学习第三天
    持续集成工具hudson【转载】
    linux-unzip命令【转载】
    javascript学习第一天
    java.util.Properties
    Eclipse快捷键【转载】
  • 原文地址:https://www.cnblogs.com/shichuan/p/4428342.html
Copyright © 2011-2022 走看看