|
楼主 |
发表于 2016-12-12 11:21:28
|
显示全部楼层
fishvip.cpp
#include <iostream>
#include <fstream>
#include<string>
#include"fishcvip.h"
FishOil::FishOil(std::string _name,std::string _uid,char _sex)
{
name=_name;
uid=_uid;
sex=_sex;
}
bool FishOil::InitFishC()
{
FishOil OilInit ("小甲鱼", "fishc00001", 'M');
if( WriteFishC(&OilInit) == false )
std::cout << "初始化失败T_T\n";
return 1;
}
bool FishOil::ReadFishC()
{
std::string temp;
std::ifstream fileInput("d:\\Fishcvip2.txt");
if( fileInput.is_open() )
{
std::cout << " 姓名 " << " 身份证 " << " 性别 " << "\n\n";
while( std::getline( fileInput, temp ) )
{
std::cout << temp << " ";
std::cout << "\n";
}
std::cout << "\n\n";
return true;
}
else
return false;
}
void FishOil::RecordFishC()
{
char goon, Save; FishOil OilData("xiao","1255",'m');
FishOil *pOilData;
goon = 'Y';
while( 'Y' == goon )
{
std::cout << "请输入鱼C账号: ";
std::cin >> OilData.name;
std::cout << "请输入鱼C身份证:";
std::cin >>OilData.uid;
std::cout << "请输入性别:";
std::cin >> OilData.sex;
std::cout << "录入成功, 请问需要保存吗?(Y/N)";
std::cin >> Save;
if( 'Y' == Save )
{
pOilData=&OilData;
if( WriteFishC( pOilData ) )
std::cout << "成功写入文件^_^\n";
else
std::cout << "写入文件失败T_T\n";
}
else
{
return;
}
std::cout << "/n请问需要再次录入吗?(Y/N)";
std::cin >> goon;
}
}
bool FishOil::WriteFishC(FishOil *pOilData)
{
std::ofstream fileOutput("FishC.txt", std::ios::app);
// std::ios::app用来说明在老数据追加新数据
if( fileOutput.is_open() )
{
fileOutput << pOilData->name << " ";
fileOutput << pOilData->uid << " ";
fileOutput << pOilData->sex << "\n";
fileOutput.close();
std::cout << "数据成功保存到FishC.txt\n\n";
}
else
{
std::cout << "保存失败T_T\n";
}
return 1;
}
bool InitFishC();
bool ReadFishC();
void RecordFishC();
bool WriteFishC(FishOil *OilData);
fishvip.h
#include <iostream>
#include <fstream>
#include<string>
class FishOil
{
private:
std::string name;
std::string uid;
char sex;
public:
FishOil(std::string name,std::string uid,char sex);
bool InitFishC();
bool ReadFishC();
void RecordFishC();
bool WriteFishC(FishOil *OilData);
};
main.cpp
#include <iostream>
#include <fstream>
#include<string>
#include"fishcvip.h"
bool InitFishC();
bool ReadFishC();
void RecordFishC();
bool WriteFishC(FishOil *OilData);
int main()
{
int i;
InitFishC(); // 初始化数据。
while( 1 )
{
std::cout << "请选择需要进行的操作: \n";
std::cout << "1. 打印数据到屏幕\n";
std::cout << "2. 录入数据\n";
std::cout << "3. 退出程序\n";
std::cin >> i;
switch( i )
{
case 1:
if( ReadFishC() )
std::cout << "成功读取文件^_^\n\n";
else
std::cout << "读取文件失败T_T\n\n";
break;
case 2:
RecordFishC();
break;
case 3:
return 0;
}
}
std::cout << "初始化失败T_T......\n\n";
return 0;
} |
|