17-1
不知道有没有理解错题意,参考list17.14
#include <iostream>int main()
{using std::cout;using std::cin;using std::endl;char ch;int count = 0;while (cin.peek() != '$'){cin.get(ch);count++;cout << ch;}cout << "\nThere are " << count << " characters before first '$'.\n";if (!cin.eof()){cin.get(ch);cout << endl << ch << " is next input character.\n";}else//好像把最后一个字符是$的时候也不会运行到这里,不知道为什么要加这个else{cout << "End of file rechaed.\n";std::exit(0);}return 0;
}
17-2
vs生成解决方案后,在命令行中打开.exe可执行文件,输入文件路径,文件内容,文件内容以CTRL+Z结束
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>int main(int argc, char* argv[])
{using namespace std;string filename;cout << "Enter name for new file: ";cin >> filename;ofstream fout(filename.c_str());char ch;if (fout.is_open()){cout << "Enter the file cotents:\n";while (cin.get(ch) && ch != EOF)fout << ch;}else{cout << "Can't open this file!";exit(EXIT_FAILURE);}fout.close();return 0;
}
17-3
换成用argc输入
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>int main(int argc, char* argv[])
{using namespace std;if (argc == 1){cerr << "\nno file\n";exit(EXIT_FAILURE);}char ch;ifstream fin(argv[1]);if (!fin.is_open()){cerr << argv[1] << "Couldn't open out file.\n";fin.clear();exit(EXIT_FAILURE);}ofstream fout(argv[2]);if (!fout.is_open()){cerr << argv[2] << " Couldn't open in file.\n";fout.clear();exit(EXIT_FAILURE);}while ((ch = fin.get()) != EOF)fout << ch;fin.close();fout.close();return 0;
}
17-4
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>int main(int argc, char* argv[])
{using namespace std;string line1, line2;ifstream fin1("C:/test1.txt");ifstream fin2("C:/test2.txt");ofstream fout("C:/test.txt");if (fin1.is_open() && fin2.is_open() && fout.is_open()){while (!fin1.eof() && !fin2.eof() && getline(fin1, line1) && getline(fin2, line2) && line1.size() > 0 && line2.size() > 0)fout << line1 << " " << line2 << endl;while (!fin1.eof() && getline(fin1, line1) && line1.size() > 0)fout << line1 << endl;while (!fin2.eof() && getline(fin2, line2) && line2.size() > 0)fout << line2 << endl;}else{cout << "Couldn't open files.";exit(EXIT_FAILURE);}fin1.close();fin2.close();fout.close();return 0;
}
17-5
跟之前一样,逐行读取,加到一起后,去重,输出
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <list>int main(int argc, char* argv[])
{using namespace std;string str;list<string> mat, pat, final;ifstream fin1("C:/test1.txt");ifstream fin2("C:/test2.txt");ofstream fout("C:/matnpat.dat");if (fin1.is_open() && fin2.is_open() && fout.is_open()){while (!fin1.eof()){getline(fin1, str);mat.push_back(str);}while (!fin2.eof()){getline(fin2, str);pat.push_back(str);}}else{cout << "Couldn't open files.";exit(EXIT_FAILURE);}mat.sort();pat.sort();final = mat;final.merge(pat);final.unique();final.sort();list<string>::iterator itor = final.begin();for (int i = 0; i < final.size(); i++)fout << *itor++ << " " << endl;fin1.close();fin2.close();fout.close();return 0;
}
17-6
enum classkind { Employee, Manager, Fink, Highfink };在test.txt中分别用0,1,2,3表示
增加了writeall和getall函数用于保存文件和输入,SetAll等其他函数仍然用的是之前写的没变
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();friend std::ostream& operator<<(std::ostream& os, const abstr_emp& e);virtual ~abstr_emp() = 0;virtual void writeall(std::ofstream& ofs);virtual void getall(std::ifstream& ifs);
};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 writeall(std::ofstream& ofs);virtual void getall(std::ifstream& ifs);
};class manager :virtual public abstr_emp
{
private:int inchargeof;
protected:void Data() const;void Get();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 writeall(std::ofstream& ofs);virtual void getall(std::ifstream& ifs);void getIncharge() {std::cout << "Enter inchargeof: ";}void writeInCharge(std::ofstream& ofs) {ofs << inchargeof << std::endl;}void readInCharge(std::ifstream& ifs){ifs >> inchargeof;}
};class fink :virtual public abstr_emp
{
private:std::string reportsto;
protected:void Data() const;void Get();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& e);virtual void ShowAll() const;virtual void SetAll();virtual void writeall(std::ofstream& ofs);virtual void getall(std::ifstream& ifs);void getReportsTo() {std::cout << "Enter reports to: ";}void writeReortsto(std::ofstream& ofs) {ofs << reportsto << std::endl;}void readReortsto(std::ifstream& ifs){ifs >> reportsto;}
};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 writeall(std::ofstream& ofs);virtual void getall(std::ifstream& ifs);
};#endif // !EMP_H_
emp.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "emp.h"
using std::cout;
using std::cin;
using std::endl;abstr_emp::~abstr_emp() {}void abstr_emp::writeall(std::ofstream& ofs)
{ofs << fname << "\n" << lname << "\n" << job << "\n";
}void abstr_emp::getall(std::ifstream& ifs)
{getline(ifs, fname);getline(ifs, lname);getline(ifs, job);
}abstr_emp::abstr_emp()
{fname = "Null";lname = "Null";job = "Null";
}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 << "Fullname: " << fname << endl;cout << "Lastname: " << lname << endl;cout << "Job: " << job << endl;
}void abstr_emp::SetAll()
{cout << "Enter firstname: ";cin >> fname;cout << "Enter lastname: ";cin >> lname;cout << "Ente job: ";cin.get();getline(cin, job);
}std::ostream& operator<<(std::ostream& os, const abstr_emp& e)
{cout << e.fname << " " << e.lname << " " << e.job << endl;return os;// TODO: 在此处插入 return 语句
}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
{abstr_emp::ShowAll();
}void employee::SetAll()
{abstr_emp::SetAll();
}void employee::writeall(std::ofstream& ofs)
{ofs << Employee << endl;abstr_emp::writeall(ofs);
}void employee::getall(std::ifstream& ifs)
{abstr_emp::getall(ifs);
}void manager::Data() const
{cout << "Inchargeof: " << inchargeof << endl;
}void manager::Get()
{cout << "Enter Inchargeof: ";cin >> inchargeof;
}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(0)
{
}manager::manager(const manager& m) : abstr_emp(m)
{inchargeof = m.inchargeof;
}void manager::ShowAll() const
{abstr_emp::ShowAll();cout << "Inchargeof: " << inchargeof << endl;
}void manager::SetAll()
{abstr_emp::SetAll();cout << "Enter inchargeof: ";cin >> inchargeof;
}void manager::writeall(std::ofstream& ofs)
{ofs << Manager << endl;abstr_emp::writeall(ofs);ofs << inchargeof << endl;
}void manager::getall(std::ifstream& ifs)
{abstr_emp::getall(ifs);ifs >> inchargeof;
}void fink::Data() const
{cout << "Reportsto: " << reportsto << endl;
}void fink::Get()
{cout << "Enter resportsto: ";cin.get();getline(cin, reportsto);
}fink::fink() :abstr_emp()
{reportsto = "Null";
}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& e) : abstr_emp(e)
{reportsto = e.reportsto;
}void fink::ShowAll() const
{abstr_emp::ShowAll();cout << "Reports to: " << reportsto << endl;
}void fink::SetAll()
{abstr_emp::SetAll();cout << "Enter reports to: ";cin.get();getline(cin, reportsto);
}void fink::writeall(std::ofstream& ofs)
{ofs << Fink << endl;abstr_emp::writeall(ofs);ofs << reportsto << endl;
}void fink::getall(std::ifstream& ifs)
{abstr_emp::getall(ifs);ifs >> reportsto;
}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
{abstr_emp::ShowAll();manager::Data();fink::Data();
}void highfink::SetAll()
{abstr_emp::SetAll();manager::Get();fink::Get();
}void highfink::writeall(std::ofstream& ofs)
{ofs << Highfink << endl;abstr_emp::writeall(ofs);manager::writeall(ofs);fink::writeReortsto(ofs);
}void highfink::getall(std::ifstream& ifs)
{abstr_emp::getall(ifs);manager::readInCharge(ifs);fink::readReortsto(ifs);
}
main.cpp
#include <iostream>
#include "emp.h"
#include <cstdlib>
#include <fstream>
const int MAX = 10;
const char* file = "c:/test.txt";int main()
{using namespace std;char ch;int i;abstr_emp* pc[MAX];ifstream fin;fin.open(file);if (!fin.is_open()){cout << "Couldn't open files!\n"; exit(EXIT_FAILURE);}int classtype;i = 0;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;}pc[i]->getall(fin);pc[i]->ShowAll();}fin.close();ofstream fout(file, ios::out | ios::app);if (!fout.is_open()){cerr << "Couldn't open file!";exit(EXIT_FAILURE);}int index = 0;cout << "Please choose the class you want to enter:\n"<< "e for employee, m for manager,\n"<< "f for fink, h for highfink,\n"<< "q to quit\n";while (cin >> ch && index < MAX){if (ch == 'q')break;switch (ch){case 'e': pc[index] = new employee;break;case 'm': pc[index] = new manager;break;case 'f': pc[index] = new fink;break;case 'h': pc[index] = new highfink;break;}pc[index]->SetAll();index++;cout << "Please choose the class you want to enter:\n"<< "e for employee, m for manager,\n"<< "f for fink, h for highfink,\n"<< "q to quit\n";}for (i = 0; i < index; i++)pc[i]->writeall(fout);fout.close();fin.clear();fin.open(file);if (fin.is_open()){cout << "Here are the contents of the " << file << " file:\n";int classtype;i = 0;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;}pc[i]->getall(fin);pc[i]->ShowAll();}fin.close();}cout << "Done.\n";return 0;
}
17.7
不知道为什么从.dat中读取的信息有乱码
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <cstdlib>
#include <algorithm>
#include <fstream>
using namespace std;class Store
{
private:ofstream& fout;
public:Store(ofstream& os) :fout(os) {}void operator()(const string& str);
};void ShowStr(const string str);
void GetStrs(ifstream& fin, vector<string>& vistr);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("C:\strings.dat", ios_base::out | ios_base::binary);for_each(vostr.begin(), vostr.end(), Store(fout));fout.close();vector<string> vistr;ifstream fin("C:\strings.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;
}void ShowStr(const string str)
{cout << str;
}void GetStrs(ifstream& fin, vector<string>& vstr)
{char* s;size_t len;while (fin.read((char*)&len, sizeof(size_t))){s = new char[len];fin.read(s, len);s[len + 1] = '\0';vstr.push_back(s);}
}void Store::operator()(const string& str)
{size_t len = str.size();fout.write((char*)&len, sizeof(size_t));fout.write(str.data(), len);
}