一、编程练习
- 编写一个程序,将键盘输入(模拟结尾)复制到指定文件中。
#include<iostream>
#include<string>
#include<fstream>
int main()
{
using namespace std;
string ch;
char put;
cout<<"Please input contents(ending of $)\n";
cin>>put;
while(put!='$')
{
ch+=put;
cin.get(put);
}
ofstream ostr;
ostr.open("loca_record.txt");
ostr<<ch;
}
- 编写一个程序,它打开两个文本文件进行输入,打开一个文本文件进行输出,该程序将两个输入文件中对应的行拼接起来,并用空格分隔,然后将结果写入到输出文件中去。如果一个文件比另一个短,则将较长文件中余下的几行直接复制到输出文件中。
#include<iostream>
#include<fstream>
#include<string>
int main()
{
using namespace std;
string file_1,file_2,file_3;
string temp1,temp2;
cout<<"Enter the first filename of reading: ";
cin>>file_1;
ifstream fin1(file_1);
cout<<"Enter the second filename of reading: ";
cin>>file_2;
ifstream fin2(file_2);
cout<<"Enter the filename of writing: ";
cin>>file_3;
ofstream fout(file_3);
int n1,n2;
if(fin1.is_open() and fin2.is_open())
{
if(fout.is_open())
{
do
{
getline(fin1,temp1);
getline(fin2,temp2);
if(!fin1 && !fin2)
break;
else if(fin1 && fin2)
fout<<temp1<<" "<<temp2<<endl;
else if(fin1 && !fin2)
fout<<temp1<<endl;
else if(!fin1 && fin2)
fout<<temp2<<endl;
}
while(fin1 || fin2);
}
else
{
cout<<"Can't open file_3";
exit(0);
}
}
else
{
cout<<"Can't open file_1 or file_2";
exit(0);
}
fin1.close();
fin2.close();
fout.close();
cout<<"Reading file_3 ...\n";
ifstream fr(file_3);
string temp;
while(fr)
{
getline(fr,temp);
cout<<temp<<endl;
}
return 0;
}
- 17_6
#include<iostream>
#include<string>
#include<fstream>
#include"employee.h"
int main()
{
using namespace std;
const string file("employee.txt");
enum classkind{Employee,Manager,Fink,Highfink};
const int MAX=2;
abstr_emp * pc[MAX];
int classtype;
ofstream fout(file,ios_base::out | ios_base::app);
for(int i=0;i<MAX;i++)
{
cout<<"Please enter classkind (Employee:0, Manager:1, Fink:2, Highfink:3 ,quit:4):\n";
cin>>classtype;
if(classtype==4)
break;
switch(classtype)
{
case Employee:
pc[i]=new employee;
break;
case Manager:
pc[i]=new manager;
break;
case Fink:
pc[i]=new fink;
break;
case Highfink:
pc[i]=new highfink;
break;
}
pc[i]->SetAll();
pc[i]->WriteAll(fout);
}
fout.close();
cout<<"Display contents of you have input: \n";
ifstream fin(file);
if (!fin.is_open())
{
cerr << "Can't open " << file << " file for output.\n";
exit(EXIT_FAILURE);
}
string ch;
while(getline(fin,ch))
cout<<ch<<endl;
fin.close();
fout.open(file,ios_base::app);
if (!fout.is_open())
{
cerr << "Can't open " << file << " file for output.\n";
exit(EXIT_FAILURE);
}
cout<<"Add data to this file and you want to insert number of data: \n";
int index;
cin>>index;
for(int i=0;i<index;i++)
{
cout<<"Please enter classkind (Employee:0, Manager:1, Fink:2, Highfink:3, quit:4):\n";
cin>>classtype;
if(classtype==4)
break;
switch(classtype)
{
case Employee:
pc[i]=new employee;
break;
case Manager:
pc[i]=new manager;
break;
case Fink:
pc[i]=new fink;
break;
case Highfink:
pc[i]=new highfink;
break;
}
pc[i]->SetAll();
pc[i]->WriteAll(fout);
}
fout.close();
fin.open(file);
if (!fin.is_open())
{
cerr << "Can't open " << file << " file for output.\n";
exit(EXIT_FAILURE);
}
string cha;
while(getline(fin,cha))
cout<<cha<<endl;
}
#ifndef EMPLOYEE_H_INCLUDED
#define EMPLOYEE_H_INCLUDED
class abstr_emp
{
std::string fname;
std::string lname;
std::string job;
public:
abstr_emp():fname("null"),lname("null"),job("null"){}
abstr_emp(const std::string & fn,const std::string & ln,const std::string & j):fname(fn),lname(ln),job(j){}
virtual void ShowAll() const;
virtual void SetAll();
virtual void WriteAll(std::ofstream & of) const;
friend std::ostream &operator<<(std::ostream & os,const abstr_emp & e);
virtual ~abstr_emp()=0;
};
class employee:public abstr_emp
{
public:
employee(): abstr_emp(){}
employee(const std::string & fn,const std::string & ln,const std::string & j):abstr_emp(fn,ln,j){}
virtual void ShowAll() const{abstr_emp::ShowAll();}
virtual void SetAll(){abstr_emp::SetAll();}
virtual void WriteAll(std::ofstream & of) const{abstr_emp::WriteAll(of);}
};
class manager: virtual public abstr_emp
{
int inchargeof;
protected:
int InChargeOf() const {return inchargeof;}
int & InChargeOf(){return inchargeof;}
public:
manager():abstr_emp(),inchargeof(0){}
manager(const std::string & fn,const std::string & ln,const std::string & j,int ico=0):abstr_emp(fn,ln,j),inchargeof(ico){}
manager(const abstr_emp & e,int ico):abstr_emp(e),inchargeof(ico){}
manager(const manager & m);
virtual void ShowAll() const;
virtual void SetAll();
virtual void WriteAll(std::ofstream & of) const;
};
class fink:virtual public abstr_emp
{
std::string reportsto;
protected:
const std::string ReportsTo() const{return reportsto;}
std::string & ReportsTo(){return reportsto;}
public:
fink():abstr_emp(),reportsto("null"){}
fink(const std::string & fn,const std::string & ln,const std::string & j,const std::string & rpo):abstr_emp(fn,ln,j),reportsto(rpo){}
fink(const abstr_emp & e,const std::string & rpo):abstr_emp(e),reportsto(rpo){}
fink(const fink & e);
virtual void ShowAll() const;
virtual void SetAll();
virtual void WriteAll(std::ofstream & of) const;
};
class highfink:public manager,public fink
{
public:
highfink(){}
highfink(const std::string & fn,const std::string & ln,const std::string & j,const std::string & rpo,int ico):abstr_emp(fn,ln,j),manager(fn,ln,j,ico),fink(fn,ln,j,rpo){}
highfink(const abstr_emp & e,const std::string & rpo,int ico):abstr_emp(e),manager(e,ico),fink(e,rpo){}
highfink(const fink & f,int ico):fink(f){InChargeOf()=ico;}
highfink(const manager & m,const std::string & rpo):abstr_emp(m),manager(m),fink(m,rpo){}
highfink(const highfink & h);
virtual void ShowAll() const;
virtual void SetAll();
virtual void WriteAll(std::ofstream & of) const;
};
void abstr_emp::ShowAll() const
{
std::cout<<"Fullname: "<<fname<<" "<<lname<<std::endl
<<"Job: "<<job<<std::endl;
}
void abstr_emp::SetAll()
{
std::cout<<"Enter first name: ";
std::cin>>fname;
std::cout<<"Enter last name: ";
std::cin>>lname;
std::cout<<"Enter job: ";
std::cin>>job;
while(std::cin.get()!='\n')
continue;
}
void abstr_emp::WriteAll(std::ofstream & of) const
{
of<<fname<<std::endl;
of<<lname<<std::endl;
of<<job<<std::endl;
}
std::ostream & operator<<(std::ostream & os,const abstr_emp & e)
{
os<<e.fname<<" "<<e.lname<<std::endl;
return os;
}
abstr_emp::~abstr_emp(){};
manager::manager(const manager & m):abstr_emp(m)
{
inchargeof=m.inchargeof;
}
void manager::ShowAll() const
{
abstr_emp::ShowAll();
std::cout<<"Inchargeof: "<<inchargeof<<std::endl;
}
void manager::SetAll()
{
abstr_emp::SetAll();
std::cout<<"Enter inchargeof: ";
std::cin>>inchargeof;
while(std::cin.get()!='\n')
continue;
}
void manager::WriteAll(std::ofstream & of) const
{
abstr_emp::WriteAll(of);
of<<inchargeof<<std::endl;
}
fink::fink(const fink & e):abstr_emp(e)
{
reportsto=e.reportsto;
}
void fink::ShowAll() const
{
abstr_emp::ShowAll();
std::cout<<"Reportsto: "<<reportsto<<std::endl;
}
void fink::SetAll()
{
abstr_emp::SetAll();
std::cout<<"Enter reportsto: ";
std::cin>>reportsto;
while(std::cin.get()!='\n')
continue;
}
void fink::WriteAll(std::ofstream & of) const
{
abstr_emp::WriteAll(of);
of<<reportsto<<std::endl;
}
highfink::highfink(const highfink & h):abstr_emp(h),manager(h),fink(h)
{
}
void highfink::ShowAll() const
{
manager::ShowAll();
std::cout<<"Reportsto: "<<ReportsTo()<<std::endl;
}
void highfink::SetAll()
{
abstr_emp::SetAll();
std::cout<<"Enter inchargeof: ";
std::cin>>InChargeOf();
std::cout<<"Enter reportsto: ";
std::cin>>ReportsTo();
while(std::cin.get()!='\n')
continue;
}
void highfink::WriteAll(std::ofstream & of) const
{
abstr_emp::WriteAll(of);
of<<manager::InChargeOf()<<std::endl;
of<<fink::ReportsTo()<<std::endl;
}
#endif
- 17_7
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
#include<cstring>
#include<algorithm>
const int LIMIT=50;
void ShowStr(const std::string & str)
{
std::cout<<str<<std::endl;
}
void GetStrs(std::ifstream & fin,std::vector<std::string> & vis)
{
unsigned long len;
while(fin.read((char *)&len,sizeof(unsigned long)))
{
char * temp=new char[len];
fin.read(temp,len);
vis.push_back(temp);
delete [] temp;
}
}
class Store
{
char * s;
std::ofstream * fout;
public:
Store(std::ofstream & os):fout(&os){s=new char[LIMIT];}
Store(const Store & st);
~Store();
char * data(const std::string & str);
bool operator()(const std::string & str);
};
Store::Store(const Store & st)
{
if(this==&st)
delete [] s;
s=new char[LIMIT];
for(int i=0;i<strlen(st.s);i++)
s[i]=st.s[i];
fout=st.fout;
}
Store::~Store()
{
delete [] s;
}
char * Store::data(const std::string & str)
{
for(int i=0; i<str.length(); i++)
{
s[i]=str[i];
}
return s;
}
bool Store::operator()(const std::string & str)
{
unsigned long len=str.size();
if(fout->is_open())
{
fout->write((char *)&len,sizeof(unsigned long));
fout->write(data(str),len);
return true;
}
else
{
exit(EXIT_FAILURE);
return false;
}
}
int main()
{
using namespace std;
vector<string> vostr;
string temp;
cout<<"Enter strings (empty line to quit):\n";
while(getline(cin,temp) && temp[0]!='\0')
vostr.push_back(temp);
cout<<"Here is your input.\n";
for_each(vostr.begin(),vostr.end(),ShowStr);
ofstream fout("string.dat",ios_base::out | ios_base::binary);
for_each(vostr.begin(),vostr.end(),Store(fout));
fout.close();
vector<string> vistr;
ifstream fin("string.dat",ios_base::in | ios_base::binary);
if(!fin.is_open())
{
cerr<<"Couldn't open file for input.\n";
exit(EXIT_FAILURE);
}
GetStrs(fin,vistr);
cout<<"\nHere are the strings read from the file:\n";
for_each(vistr.begin(),vistr.end(),ShowStr);
return 0;
}