main.cpp

 #include <stdio.h>
#include <stdlib.h>
#include "data.h"
#include "ctrl.h" int main()
{
PPASSWORD pPwd = nullptr; // 密码数组
int nCount = ; // 当前密码个数
g_nNum = ; // 当前密码本容量 readInfo(&pPwd, &nCount); // 从本地文件中读取密码,用于初始化密码本 while (true)
{
system("title 欢迎使用密码本");
system("color 0E");
printf("**************************欢迎使用密码本****************************\n");
printf("* *\n");
printf("* --------------------------------- *\n");
printf("* | Powered By NoThx | *\n");
printf("* --------------------------------- *\n");
printf("* *\n");
printf("* 请输入你的选择 *\n");
printf("* 1)查看所有 *\n");
printf("* 2)新增密码 *\n");
printf("* 3)删除密码 *\n");
printf("* 4)修改密码 *\n");
printf("* 5)查询密码 *\n");
printf("* 6)生成文件 *\n");
printf("* 0)退出软件 *\n");
printf("* *\n");
printf("******************************************************************\n"); OPTION op;
scanf_s("%d", &op); switch (op)
{
case 查看所有:
searchAllInfo(pPwd, nCount);
break;
case 新增:
insertInfo(pPwd, &nCount);
break;
case 删除:
deleteInfo(pPwd, &nCount);
break;
case 修改:
alterInfo(pPwd, nCount);
break;
case 查询:
searchInfo(pPwd, nCount);
break;
case 保存文本:
SaveInfoA(pPwd, nCount);
break;
case 退出软件:
exit();
break;
default:
break;
}
} return ; }
 data.h

 #pragma once

 //#define PWD_LEN 20  //密码最大长度

 // 自定义数据类型
//定义所有的用户操作为枚举
enum OPTION {
#define 退出软件 0
#define 查看所有 1
#define 新增 2
#define 删除 3
#define 修改 4
#define 查询 5
#define 保存文本 6
#define 保存二进制 7
}; // 定义存储账号密码的结构体
typedef struct _PASSWORD
{
char website[]; // 站点名
char userName[]; // 用户名
char passWord[]; // 密码
char note[]; // 备注
}PASSWORD, *PPASSWORD; // 全局变量, 常量的声明
#define NUM 100 // 初始密码本的容量
extern int g_nNum; // 当前密码本容量
 data.cpp

 #include "data.h"

 // 全局变量的定义
int g_nNum;
 ctrl.h

 #pragma once

 //查看所有密码
void searchAllInfo(PASSWORD pwd[], int nCount); //新增密码
void insertInfo(PASSWORD pwd[], int *pnCount); //删除密码
void deleteInfo(PASSWORD pwd[], int * pnCount); //修改密码
void alterInfo(PASSWORD pwd[], int nCount); //查询密码
void searchInfo(PASSWORD pwd[], int nCount); // 保存密码(文本方式)
void SaveInfoA(PASSWORD pwd[], int nCount); //保存密码(二进制的方式)
void SaveInfoB(PASSWORD pwd[], int nCount); //从本地文件中读取密码,用于初始化密码本
void readInfo(PPASSWORD *ppPwd, int * pnCount);// 传递指针是因为希望函数在内部改变这个变量的值
 ctrl.cpp

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "data.h" // 函数声明,否则保存不可用
void SaveInfoB(PASSWORD pwd[], int nCount); //查看所有密码
void searchAllInfo(PASSWORD pwd[], int nCount)
{
printf("==共计:%3d 条记录=====================\n", nCount);
for (int i = ; i < nCount; i++)
{
printf("--ID:%3d---------------------\n", i);
printf("目标:%s \n", pwd[i].website);
printf("用户名:%s \n", pwd[i].userName);
printf("密码:%s \n", pwd[i].passWord);
printf("备注: %s \n", pwd[i].note);
} printf("\n");
system("pause");
system("cls");
} //新增密码
void insertInfo(PASSWORD pwd[], int *pnCount)
{
int i = *pnCount;
if (i < || i >= NUM)
{
printf("插入位置异常:%d \n", i);
return;
}
printf("请输入<目标站点>\n");
scanf_s("%s", pwd[i].website, );
printf("请输入<用户名>\n");
scanf_s("%s", pwd[i].userName, );
printf("请输入<密码>\n");
scanf_s("%s", pwd[i].passWord, );
printf("请输入<备注信息>\n");
scanf_s("%s", pwd[i].note, ); (*pnCount)++; SaveInfoB(pwd, *pnCount); printf("\n");
system("pause");
system("cls"); } //删除密码
void deleteInfo(PASSWORD pwd[], int * pnCount)
{
int i = -;
printf("请输入待删除项<密码ID>\n");
scanf_s("%d", &i);
if (i < || i >= NUM)
{
printf("删除位置异常:%d \n", i);
return;
} for (int j = i; j < *pnCount - ; j++)
{
pwd[j] = pwd[j + ];
} (*pnCount)--; SaveInfoB(pwd, *pnCount); printf("\n");
system("pause");
system("cls");
} //修改密码
void alterInfo(PASSWORD pwd[], int nCount)
{
int i = -;
printf("请输入待修该项<密码ID>\n");
scanf_s("%d", &i);
if (i < || i >= NUM)
{
printf("修改位置异常:%d \n", i);
return;
} printf("--ID:%3d---------------------\n", i);
printf("目标:%s \n", pwd[i].website);
printf("用户名:%s \n", pwd[i].userName);
printf("密码:%s \n", pwd[i].passWord);
printf("备注: %s \n", pwd[i].note);
printf("~修改~\n"); printf("请输入<目标站点>\n");
scanf_s("%s", pwd[i].website, );
printf("请输入<用户名>\n");
scanf_s("%s", pwd[i].userName, );
printf("请输入<密码>\n");
scanf_s("%s", pwd[i].passWord, );
printf("请输入<备注信息>\n");
scanf_s("%s", pwd[i].note, ); SaveInfoB(pwd, nCount); printf("\n");
system("pause");
system("cls");
} //查询密码
void searchInfo(PASSWORD pwd[], int nCount)
{
char key[] = { };
printf("请输入查找密码的关键字:\n");
scanf_s("%s", key, ); // 找出所有匹配项
int findPwd[] = { };
int nfindCount = ;
for (int i = ; i < nCount; i++)
{
if (strstr(pwd[i].website, key))
{
findPwd[nfindCount++] = i;
continue;
}
if (strstr(pwd[i].userName, key))
{
findPwd[nfindCount++] = i;
continue;
}
if (strstr(pwd[i].passWord, key))
{
findPwd[nfindCount++] = i;
continue;
}
if (strstr(pwd[i].note, key))
{
findPwd[nfindCount++] = i;
continue;
}
} //打印所有匹配项
printf("==共计匹配:%3d 条记录===================\n", nfindCount);
for (int i = ; i < nfindCount; i++)
{
int pos = findPwd[i];
printf("--ID:%3d----------------------\n", pos);
printf("目标:%s \n", pwd[pos].website);
printf("用户名:%s \n", pwd[pos].userName);
printf("密码:%s \n", pwd[pos].passWord);
printf("备注:%s \n", pwd[pos].note);
} printf("\n");
system("pause");
system("cls");
} // 保存密码(文本方式)
void SaveInfoA(PASSWORD pwd[], int nCount)
{
/*
读写文件的固定套路
1. 打开文件
2. 读写文件
3. 关闭文件
*/ // 打开文件
FILE *pFile;
errno_t err = fopen_s(&pFile, "data.txt", "w");
if (err != )
{
return;
} // 读写文件
fprintf(pFile, "==共计:%3d 条记录==================\n", nCount);
for (int i = ; i < nCount; i++)
{
fprintf(pFile, "目标: %s \n", pwd[i].website);
fprintf(pFile, "用户名: %s \n", pwd[i].userName);
fprintf(pFile, "密码: %s \n", pwd[i].passWord);
fprintf(pFile, "备注: %s \n", pwd[i].note);
} // 关闭文件
fclose(pFile);
printf("保存文本密码成功,使用后请尽快删除!!\n");
system("pause");
system("cls");
} //保存密码(二进制的方式)
void SaveInfoB(PASSWORD pwd[], int nCount)
{
// 打开文件
FILE *pFile;
errno_t err = fopen_s(&pFile, "data.i", "wb");
if (err != )
{
return;
} // 读写文件
fwrite(&nCount, sizeof(nCount), , pFile); // 当前密码个数
fwrite(&g_nNum, sizeof(g_nNum), , pFile); // 当前密码本容量(个数) //先加密(使用中间文件),在存储
int byteSize = sizeof(PASSWORD) * g_nNum;
char *buf = (char *)malloc(byteSize);
memcpy_s(buf, byteSize, pwd, byteSize); for (int i = ; i < byteSize; i++)
{
buf[i] ^= (i + nCount + g_nNum);
}
size_t rlen = fwrite(buf, sizeof(PASSWORD), g_nNum, pFile); // 密码数组
if (rlen != g_nNum)
{
printf("写入密码文件错误\n");
}
else
{
printf("成功!!\n");
} // 关闭文件
fclose(pFile);
free(buf);
} //从本地文件中读取密码,用于初始化密码本
void readInfo(PPASSWORD *ppPwd, int * pnCount)
{
// 打开文件
FILE *pFile;
errno_t err = fopen_s(&pFile, "data.i", "rb");
if (err != )
{
*ppPwd = (PPASSWORD)malloc(sizeof(PASSWORD) * NUM);
g_nNum = NUM;
return;
} // 读写文件
// 先读取,再解密
fread(pnCount, sizeof(int), , pFile); // 当前密码个数
fread(&g_nNum, sizeof(g_nNum), , pFile); //当前密码本容量(个数)
*ppPwd = (PPASSWORD)malloc(sizeof(PASSWORD) * g_nNum);
memset(*ppPwd, , sizeof(PASSWORD)*g_nNum);
size_t rlen = fread(*ppPwd, sizeof(PASSWORD), g_nNum, pFile); // 密码数组 if (rlen != g_nNum)
{
printf("读取密码文件错误 \n");
fclose(pFile);
return;
} // typedef unsigned int size_t; int byteSize = sizeof(PASSWORD)*g_nNum;
char *buf = (char *)*ppPwd;
for (int i = ; i < byteSize; i++)
{
buf[i] ^= (i + *pnCount + g_nNum);
} // 关闭文件
fclose(pFile);
}

运行结果如下:

密码本(无bug版)的更多相关文章

  1. 【资源】C++学习资料 - 逆天整理 - 精华无密版【最新】

    再失效就太无语了,链接都是多份的~~—————————————————基础——————————————C++环境搭建(全套)http://pan.baidu.com/s/1o6y0smY链接:http ...

  2. 漫谈程序猿系列:无BUG不生活

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9ydW9r/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  3. 【web 安全测试Tools】BurpSuite 1.7.32及注册机【无后门版】

    BurpSuite 1.7.32 原版+注册机 下载 链接:https://pan.baidu.com/s/1LFpXn2ulTLlcYZHG5jEjyw 密码:mie3 注意无后门版文件完整性: b ...

  4. 佛祖保佑 永无bug

    /* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : | ...

  5. 佛祖保佑,永无bug

    /* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : | ...

  6. 佛祖保佑 永无BUG 永不修改

    // // _oo0oo_ // o8888888o // 88" . "88 // (| -_- |) // 0\ = /0 // ___/`---'\___ // .' \\| ...

  7. 职业卖家淘宝美工教程,掌握技能无师自通 学到的不只是PS(共81节)附素材【无水印版】

    职业卖家淘宝美工教程,掌握技能无师自通 学到的不只是PS(共81节)附素材[无水印版]设计传说出品的专业课程是我们资深培训讲师精心录制的,只有视频教程和常用必备的插件,其他绝不掺和,如果你是职业卖家, ...

  8. 佛主保佑,永无bug

    /*                    _ooOoo_                   o8888888o                   88" . "88      ...

  9. 佛祖保佑 永无BUG(网转 by atkfc)

        //                   _ooOoo_    //                  o8888888o    //                  88" . ...

随机推荐

  1. 《剑指offer》---顺时针打印矩阵

    本文算法使用python3实现 1. 问题1 1.1 题目描述:   输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 ...

  2. UVA 167 R-The Sultan's Successors

    https://vjudge.net/contest/68264#problem/R The Sultan of Nubia has no children, so she has decided t ...

  3. xpath教程二 ---- 通过ID和Class检索

    必备知识点 在html中,id是唯一的 在html中,class是可以多处引用的 工具 Python3版本 lxml库[优点是解析快] HTML代码块[从网络中获取或者自己杜撰一个] requests ...

  4. Zabbix监控配置

    Zabbix在线文档 https://www.zabbix.com/documentation/4.0/zh/manual/config/hosts 1.我们启动服务后,我们看到了端口都正在监听,但是 ...

  5. AutoHotKey 快速入门

    AutoHotKey 是一个免费的键盘宏程序,可以用于配置键盘快捷键.鼠标事件 以及摇杆事件,还可以在输入文本的时候对文本进行扩展(自动补全) 第一个脚本 新建文件test.ahk并输入以下内容: ^ ...

  6. Redis 学习之数据类型

    该文使用centos6.5 64位 redis-3.2.8 [root@localhost bin]# netstat -tunpl |grep 6379  查看redis 是否启动成功 一.Stri ...

  7. [剑指Offer] 61.序列化二叉树

    题目描述 请实现两个函数,分别用来序列化和反序列化二叉树 /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *r ...

  8. Java中的缓冲流详解

    缓冲流增强了读写文件的能力,比如Student.txt是一个学生的名单,每个姓名占一行.如果我们想要读取名字,那么每次必须读取一行,使用FileReader流很难完成这样的任务,因为我们不清楚一行有多 ...

  9. 《高性能MySQL》 读书总结

    目录: 第一章.MySQL架构与历史 第二章.MySQL基准测试 第三章.服务器性能剖析 第四章.Schema与数据类型优化 第五章.创建高性能的索引 第六章.查询性能优化 第七章.MySQL高级特性 ...

  10. 【题解】FJOI2015火星商店问题

    好几天之前做的题目了,一直想写一下博客也没腾出时间来,今天赶紧把坑给填上呼呼呼~ 这道题首先如果只考虑每个商店中没有时间限制的物品时,我们只需要使用一棵可持久化trie树来维护区间内的异或最大值即可, ...