题目
1.编写一个程序计算输入流中第一个$之前的字符数目,并将 $留在输入流中。
2.编写一个程序,将键盘输入(直到模拟的文件尾)复制到通过命令行指定的文件中。
3.编写一个程序,将一个文件复制到另一个文件中。让程序通过命令行获取文件名。如果文件无法打开,程序将指出这一点。
4.编写一个程序,它打开两个文本文件进行输入,打开一个文本文件进行输出。该程序将两个输入文件中对应的行并接起来,并用空格分隔,然后将结果写入到输出文件中。如果一个文件比另一个短,则将较长文件中余下的几行直接复制到输出文件中。后例省略。
5.Mat和Pat想邀请他们的朋友来参加派对,就像第16章中的编程练习8那样,但现在他们希望程序使用文件。他们请您编写一个完成下述任务的程序。
- 文本文件mat.dat中读取Mat朋友的姓名清单,其中每行为一个朋友。姓名将被存储在容器,然后按顺序显示出来。
- 从文本文件pat.dat中读取Pat朋友的姓名清单,其中每行为一个朋友。姓名将被存储在容器,然后按顺序显示出来。
- 合并两个清单,删除重复的条目,并将结果保存在文件matnpat.dat中,其中每行为一个朋友。
6.考虑14章的编程练习5中的类定义。如果还没有完成这个练习,请现在就做,然后完成下面的任务。
编写一个程序,它使用标准C++I/O、文件I/O以及14章的编程练习5中定义的employee、manager、fink和highfink类型的数据。该程序应包含程序清单17.17中的代码行,即允许用户将新数据添加到文件中。该程序首次被运行时,将要求用户输入数据,然后显示所有的数据,并将这些信息保存到一个文件中。当该程序再次被运行时,将首先读取并显示文件中的数据,然后让用户添加数据,并显示所有的数据。差别之一是,应通过一个指向employee类型的指针数组来处理数据。这样,指针可以指向employee对象,也可以指向从employee派生出来的其他三种对象中的任何一种。使数组较小有助于检查程序,例如,您可能将数组限定为最多包含10个元素:(后面题目省略)
7.下面是某个程序的部分代码。该程序将键盘输入读取到一个由string对象组成的vector中,将字符串内容(而不是string对象)存储到一个文件中,然后该文件的内容复制到另一个由string对象组成的vector中。(后面省略)
程序
#include <iostream>
int main()
{
using namespace std;
cout<<"Please enter some character:\n";
char ch;
int count=0;
cin.get(ch);
while(ch!='$')
{
count++;
cin.get(ch);
}
cout<<"There are "<<count<<" characters.\n";
return 0;
}
2.该题需要将文本文件添加到命令参数中,我用的编译器为Dev-C++,添加方法如下:运行—>参数,弹出添加信息如下图所示:
//需要将文本文件添加到命令参数中
//count.cpp---17.17
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main(int argc, char * argv[])
{
using namespace std;
ofstream fout;
for (int file = 1; file < argc; file++)
{
fout.open(argv[file]);
if (!fout.is_open())
{
cerr << "Can't open " << argv[file] << endl;
exit(EXIT_FAILURE);
}
cout << "Please enter your input (enter a blank line to quit): \n";
string input;
while (getline(cin, input) && input.size() > 0)
fout << input << endl;
cout << "Input over.\nBye.\n";
fout.close();
}
return 0;
}
3.命令行参数需填入复制文件及待复制文件名
#include <iostream>
#include <fstream>
#include <cstdlib>
int main(int argc,char * argv[])
{
using namespace std;
ifstream fin;
ofstream fout;
char ch;
fin.open(argv[1]);
fout.open(argv[2]);
if(!fin.is_open())
{
cerr<<"could not open "<<argv[1]<<endl;
fin.clear();
exit(EXIT_FAILURE);
}
if(!fout.is_open())
{
cerr<<"could not open "<<argv[2]<<endl;
fout.close();
exit(EXIT_FAILURE);
}
cout<<"Open file successfully.\n";
cout<<"Begin to copy and paste.\n";
while(fin.get(ch))
fout<<ch;
fin.clear();
fin.close();
fout.close();
cout<<"Done.\n";
return 0;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
int main()
{
using namespace std;
ifstream fin1,fin2;
ofstream fout;
fin1.open("input1.txt");
fin2.open("input2.txt");
fout.open("output.txt");
if(!fin1.is_open())
{
cerr<<"could not open input1.txt.\n";
fin1.clear();
fin2.clear();
fout.close();
exit(EXIT_FAILURE);
}
if(!fin2.is_open())
{
cerr<<"could not open input2.txt.\n";
fin1.clear();
fin2.clear();
fout.close();
exit(EXIT_FAILURE);
}
if(!fout.is_open())
{
cerr<<"could not open output.txt.\n";
fin1.clear();
fin2.clear();
fout.close();
exit(EXIT_FAILURE);
}
cout<<"Open file successfully.\n";
cout<<"Begin to read and write.\n";
string str1,str2;
while(!fin1.eof()&&!fin2.eof())
{
getline(fin1,str1);
getline(fin2,str2);
fout<<str1<<" "<<str2<<endl;
}
while(!fin1.eof())
{
getline(fin1,str1);
fout<<str1<<endl;
}
while(!fin2.eof())
{
getline(fin2,str2);
fout<<str2<<endl;
}
fin1.close();
fin2.close();
fout.close();
cout<<"Done.\n";
return 0;
}
#include <iostream>
#include <fstream>
#include <set>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <iterator>
int main()
{
using namespace std;
ifstream fin1,fin2;
ofstream fout;
fin1.open("mat.dat");
fin2.open("pat.dat");
fout.open("matnpat.dat");
if(!fin1.is_open())
{
cerr<<"could not open mat.dat.\n";
exit(EXIT_FAILURE);
}
if(!fin2.is_open())
{
cerr<<"could not open pat.dat.\n";
exit(EXIT_FAILURE);
}
if(!fout.is_open())
{
cerr<<"could not open matnpat.dat.\n";
exit(EXIT_FAILURE);
}
string temp;
set<string> mat,pat,matnpat;
while(getline(fin1,temp))
mat.insert(temp);
ostream_iterator<string,char> output(cout,"\n");
cout<<"Mat's friends:\n";
copy(mat.begin(),mat.end(),output);
while(getline(fin2,temp))
pat.insert(temp);
cout<<"\nPat's friends:\n";
copy(pat.begin(),pat.end(),output);
ostream_iterator<string,char> out(fout,"\n");
set_union(mat.begin(),mat.end(),pat.begin(),pat.end(),
insert_iterator<set<string>> (matnpat,matnpat.begin()));
cout<<"\nMat and Pat's friends:\n";
copy(matnpat.begin(),matnpat.end(),output);
copy(matnpat.begin(),matnpat.end(),out);
return 0;
}
//emp.h
#ifndef EMP_H_
#define EMP_H_
#include <iostream>
#include <string>
#include <fstream>
enum classkind{Employee,Manager,Fink,Highfink};
class abstr_emp
{
private:
std::string fname;
std::string lname;
std::string job;
public:
abstr_emp();
abstr_emp(const std::string & fn, const std::string & ln, const std::string & j);
virtual void ShowAll() const;
virtual void SetAll();
virtual void setall(std::ifstream &fin);
virtual void writeall(std::ofstream &fout);
friend std::ostream & operator<<(std::ostream & os, const abstr_emp & e);
virtual ~abstr_emp() = 0;
};
class employee : public abstr_emp
{
public:
employee();
employee(const std::string & fn, const std::string & ln, const std::string & j);
virtual void ShowAll() const;
virtual void SetAll();
virtual void setall(std::ifstream &fin);
virtual void writeall(std::ofstream &fout);
};
class manager : virtual public abstr_emp
{
private:
int inchargeof;
protected:
int InChargeOf() const { return inchargeof; }
int & InChargeOf() { return inchargeof; }
public:
manager();
manager(const std::string & fn, const std::string & ln, const std::string & j, int ico = 0);
manager(const abstr_emp & e, int ico);
manager(const manager & m);
virtual void ShowAll() const;
virtual void SetAll();
virtual void setall(std::ifstream &fin);
virtual void writeall(std::ofstream &fout);
};
class fink : virtual public abstr_emp
{
private:
std::string reportsto;
protected:
const std::string ReportsTo() const { return reportsto; }
std::string & ReportsTo() { return reportsto; }
public:
fink();
fink(const std::string & fn, const std::string & ln, const std::string & j, const std::string & rpo);
fink(const abstr_emp & e, const std::string & rpo);
fink(const fink & f);
virtual void ShowAll() const;
virtual void SetAll();
virtual void setall(std::ifstream &fin);
virtual void writeall(std::ofstream &fout);
};
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);
highfink(const abstr_emp & e, const std::string & rpo, int ico);
highfink(const fink & f, int ico);
highfink(const manager & m, const std::string & rpo);
highfink(const highfink & h);
virtual void ShowAll() const;
virtual void SetAll();
virtual void setall(std::ifstream &fin);
virtual void writeall(std::ofstream &fout);
};
#endif
//emp.cpp
#include "emp.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
abstr_emp::abstr_emp()
{
fname="None";
lname="None";
job="None";
}
abstr_emp::abstr_emp(const std::string & fn, const std::string & ln, const std::string & j)
{
fname=fn;
lname=ln;
job=j;
}
void abstr_emp::ShowAll() const
{
cout<<"First name: "<<fname<<endl;
cout<<"Last name: "<<lname<<endl;
cout<<"Job: "<<job<<endl;
}
void abstr_emp::SetAll()
{
cout<<"Enter the first name: ";
getline(cin,fname);
cout<<"Enter the last name: ";
getline(cin,lname);
cout<<"Enter the job: ";
getline(cin,job);
}
void abstr_emp::setall(ifstream &fin)
{
getline(fin,fname);
getline(fin,lname);
getline(fin,job);
}
void abstr_emp::writeall(ofstream &fout)
{
fout<<fname<<endl<<lname<<endl<<job<<endl;
}
std::ostream & operator<<(std::ostream & os, const abstr_emp & e)
{
os<<"First name: "<<e.fname<<endl;
os<<"Last name: "<<e.lname<<endl;
return os;
}
abstr_emp::~abstr_emp()
{
}
employee::employee():abstr_emp()
{
}
employee::employee(const std::string & fn, const std::string & ln, const std::string & j)
:abstr_emp(fn,ln,j){}
void employee::ShowAll() const
{
cout<<"Employee:\n";
abstr_emp::ShowAll();
}
void employee::SetAll()
{
cout<<"Employee:\n";
abstr_emp::SetAll();
}
void employee::setall(ifstream &fin)
{
abstr_emp::setall(fin);
}
void employee::writeall(ofstream &fout)
{
fout<<Employee<<endl;
abstr_emp::writeall(fout);
}
manager::manager():abstr_emp()
{
inchargeof=0;
}
manager::manager(const std::string & fn, const std::string & ln, const std::string & j, int ico)
:abstr_emp(fn,ln,j),inchargeof(ico)
{
}
manager::manager(const abstr_emp & e, int ico)
:abstr_emp(e),inchargeof(ico)
{
}
manager::manager(const manager & m):abstr_emp(m)
{
inchargeof=m.inchargeof;
}
void manager::ShowAll() const
{
cout<<"Manager: \n";
abstr_emp::ShowAll();
cout<<"In charge of "<<inchargeof<<" employees.\n";
}
void manager::SetAll()
{
cout<<"Manager: \n";
abstr_emp::SetAll();
cout<<"In charge of: ";
cin>>inchargeof;
cin.get();
}
void manager::setall(ifstream &fin)
{
abstr_emp::setall(fin);
fin>>inchargeof;
fin.ignore();
}
void manager::writeall(ofstream &fout)
{
fout<<Manager<<endl;
abstr_emp::writeall(fout);
fout<<inchargeof<<endl;
}
fink::fink():abstr_emp()
{
reportsto="None";
}
fink::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::fink(const abstr_emp & e, const std::string & rpo)
:abstr_emp(e),reportsto(rpo)
{
}
fink::fink(const fink & f):abstr_emp(f)
{
reportsto=f.reportsto;
}
void fink::ShowAll() const
{
cout<<"Fink:\n";
abstr_emp::ShowAll();
cout<<"Reports to "<<reportsto<<endl;
}
void fink::SetAll()
{
cout<<"Fink:\n";
abstr_emp::SetAll();
cout<<"Enter reports to: ";
getline(cin,reportsto);
}
void fink::setall(ifstream &fin)
{
abstr_emp::setall(fin);
fin>>reportsto;
fin.ignore();
}
void fink::writeall(ofstream &fout)
{
fout<<Fink<<endl;
abstr_emp::writeall(fout);
fout<<reportsto<<endl;
}
highfink::highfink():abstr_emp(),manager(),fink()
{
}
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::highfink(const abstr_emp & e, const std::string & rpo, int ico)
:abstr_emp(e),manager(e,ico),fink(e,rpo)
{
}
highfink::highfink(const fink & f, int ico)
:abstr_emp(f),manager(f,ico),fink(f)
{
}
highfink::highfink(const manager & m, const std::string & rpo)
:abstr_emp(m),manager(m),fink(m,rpo)
{
}
highfink::highfink(const highfink & h)
:abstr_emp(h),manager(h),fink(h)
{
}
void highfink::ShowAll() const
{
cout<<"Highfink:\n";
abstr_emp::ShowAll();
cout<<"In charge of: "<<manager::InChargeOf()<<endl;
cout<<"Reports to: "<<fink::ReportsTo()<<endl;
cout<<endl;
}
void highfink::SetAll()
{
cout<<"Highfink:\n";
abstr_emp::SetAll();
cout<<"In charge of: ";
cin>>manager::InChargeOf();
cin.get();
cout<<"Reports to: ";
getline(cin,fink::ReportsTo());
}
void highfink::setall(ifstream &fin)
{
abstr_emp::setall(fin);
fin>>manager::InChargeOf();
fin.ignore();
fin>>fink::ReportsTo();
fin.ignore();
}
void highfink::writeall(ofstream &fout)
{
fout<<Highfink<<endl;
abstr_emp::writeall(fout);
fout<<manager::InChargeOf()<<endl;
fout<<fink::ReportsTo()<<endl;
}
//main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include "emp.h"
using namespace std;
const int MAX=10;
const char * file="emp.txt";
int main()
{
abstr_emp *pc[MAX];
ifstream fin;
int classtype;
int i=0;
char ch;
fin.open(file);
if(fin.is_open())
{
cout<<"Here are the current contents of the "
<<file<<" file:\n";
while((fin>>classtype).get(ch))
{
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;
default: cerr<<"Warning: Type error!\n"; break;
}
pc[i]->setall(fin);
pc[i]->ShowAll();
i++;
}
fin.close();
}
ofstream fout(file,ios::out|ios::app);
if(!fout.is_open())
{
cerr<<"Can't open "<<file<<endl;
exit(EXIT_FAILURE);
}
int index=0;
cout<<"\nPlaease enter the class type:\n"
<<"0)Employee\t1)Manager\t2)Fink\t3)Highfink\tq)Quit\n";
while(cin>>classtype&&index<MAX)
{
cin.ignore();
switch(classtype)
{
case 0: pc[index]=new employee;
pc[index]->SetAll();
break;
case 1: pc[index]=new manager;
pc[index]->SetAll();
break;
case 2: pc[index]=new fink;
pc[index]->SetAll();
break;
case 3: pc[index]=new highfink;
pc[index]->SetAll();
break;
default:cerr<<"Warning: Type error!\n";
break;
}
index++;
cout<<"\nPlaease enter the class type:\n"
<<"0)Employee\t1)Manager\t2)Fink\t3)Highfink\tq)Quit\n";
}
cout<<"Input over.\nBegin to write into the file.\n";
for(int i=0;i<index;i++)
{
pc[i]->writeall(fout);
}
fout.close();
cout<<"Write over.\n";
fin.clear();
fin.open(file);
if(fin.is_open())
{
cout<<"Here are the current contents of the "
<<file<<" file:\n\n";
while((fin>>classtype).get(ch))
{
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;
default: cerr<<"Warning: Type error!\n"; break;
}
pc[i]->setall(fin);
pc[i]->ShowAll();
i++;
}
fin.close();
}
return 0;
}
//不太会,参考了一些网上的做法
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
#include <algorithm>
using namespace std;
void ShowStr(const string &s);
void GetStrs(istream &is,vector<string> &vstr);
class Store
{
private:
ofstream & of;
public:
Store(ofstream & out):of(out){}
void operator()(const string &str);
};
void Store::operator()(const string &str)
{
size_t len=str.size();
of.write((char*)&len,sizeof(size_t));
of.write(str.data(),len);
}
int main()
{
vector<string> vostr;
string temp;
cout << "Enter string (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("strings.dat", ios_base::out | ios_base::binary);
for_each(vostr.begin(), vostr.end(), Store(fout));
fout.close();
vector<string> vistr;
ifstream fin("strings.dat", ios_base::in | ios_base::binary);
if (!fin.is_open())
{
cerr << "Could not 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;
}
void ShowStr(const string &s)
{
cout<<s<<endl;
}
void GetStrs(istream &is,vector<string> &vstr)
{
size_t len;
while(is.read((char *)&len,sizeof(size_t)))
{
char *temp=new char[len];
is.read(temp,len);
temp[len+1]='\0';
vstr.push_back(temp);
}
}