![]() |
#2
xxlovemf2010-10-01 15:29
|

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;//注册账号(用户名与密码)
int main(int argv,char **argc)
{
string name,passwd,total;
ofstream os("d:\data.dat",ios::app);//创建文件 以追加的方式
string::size_type pos;//子串的位置
if(!os)
{
cerr<<"ofstream error!"<<endl;
exit(1);
}
cout<<"------Register Name------"<<endl;
cout<<"please input your name:"<<endl;
cin>>name;
ifstream is("d:\data.dat");//读取文件
is >> total;//表示文件中的所有字符串
pos=total.find(name);//用户名在文件中的位置
if(pos != -1)//若用户名存在
{
cout<<"this name is exist!"<<endl;
}
else//若用户名不存在
{
cout<<"please input your passwd:"<<endl;
cin>>passwd;
os<<name<<":"<<passwd<<",";//保存用户名与字符串
cout<<"------register success------!"<<endl;
}
is.close();
os.close();
return 0;
}
写得比较简陋,有望大家指出意见!!!!#include <fstream>
#include <string>
#include <windows.h>
using namespace std;//注册账号(用户名与密码)
int main(int argv,char **argc)
{
string name,passwd,total;
ofstream os("d:\data.dat",ios::app);//创建文件 以追加的方式
string::size_type pos;//子串的位置
if(!os)
{
cerr<<"ofstream error!"<<endl;
exit(1);
}
cout<<"------Register Name------"<<endl;
cout<<"please input your name:"<<endl;
cin>>name;
ifstream is("d:\data.dat");//读取文件
is >> total;//表示文件中的所有字符串
pos=total.find(name);//用户名在文件中的位置
if(pos != -1)//若用户名存在
{
cout<<"this name is exist!"<<endl;
}
else//若用户名不存在
{
cout<<"please input your passwd:"<<endl;
cin>>passwd;
os<<name<<":"<<passwd<<",";//保存用户名与字符串
cout<<"------register success------!"<<endl;
}
is.close();
os.close();
return 0;
}
[ 本帖最后由 xxlovemf 于 2010-10-1 15:30 编辑 ]