参考文献《大道至简:C++STL》
- 简单操作
#include#include using namespace std; void scicout(const double& x){ cout.scientific; cout.precision(6); cout << x << endl; } int main(){ double d = 12345678.5678901234; scicout(d); //1.23457e+07 #if false ios::iostate olde = cin.exceptions(); cout << "Old exceptions: " << olde << endl; int x = 0; try{ cin.exceptions(ios::failbit | ios::eofbit | ios::badbit); cin >> x; } catch(const std::exception& e){ std::cerr << e.what() << 'n'; } #endif #if false char chdim[] = {0, 0, 0, 0, 0, 0, 0}; cin.get(chdim, 6, 'n'); cout.write(chdim, 6); cout << endl; cin.get(); cin.getline(chdim, 6, 'n'); cout.write(chdim, 6); cout << endl; cin.get(); #endif return 0; }
- 缓冲区迭代器
#include#include #include using namespace std; int main(){ string str("VT&y8V& tHUBUy .n"); ostreambuf_iterator bufwrite(cout); copy(str.begin(), str.end(), bufwrite); istreambuf_iterator inpos(cin); istreambuf_iterator endpos; ostreambuf_iterator outpos(cout); while (inpos != endpos){ *outpos = *inpos; inpos++; outpos++; } return 0; }
- 自定义缓冲区
#include#include #include using namespace std; class outbuf : public streambuf { public: virtual int_type overflow(int_type c) { if(c != EOF) { c = toupper(c, getloc()); if(putchar(c) == EOF) { return EOF; } } return c; } }; int main() { outbuf ob; ostream out(&ob); int num = 56; out << hex << showbase << num << endl; return 0; }
- 文件流
#include#include #include using namespace std; int main() { string filename; cin >> filename; ofstream fout(filename.c_str()); fout << "Rand Number : " << rand() << endl;; fout.close(); ifstream fin(filename.c_str()); char ch; while (fin.get(ch)) { cout << ch; } fin.close(); ifstream fin2(filename.c_str()); cout << fin2.rdbuf(); fin2.close(); return 0; }
- 文件读写
#include#include #include using namespace std; const string filename = "test"; int main() { ifstream fin; fin.open(filename.c_str(), ios_base::in); if(fin.is_open()) { cout << fin.rdbuf(); } fin.close(); ofstream fout; fout.open(filename.c_str(), ios_base::out | ios_base::app); if(!fout.is_open()) { cout << "file open filed" << endl; exit(EXIT_FAILURE); } string buffer; while(getline(cin, buffer) && buffer.size()) { fout << buffer << endl; } fout.close(); fin.open(filename.c_str(), ios_base::in); if(fin.is_open()) { cout << fin.rdbuf(); } fin.close(); return 0; }
- 二进制文件读写
#include#include #include #include #include #define BUFFER_SIZE 256 using namespace std; const string filename = "log.dat"; int main() { ifstream ifs; ofstream ofs; ifs.open(filename.c_str(), ios_base::in | ios_base::binary); char *buf = new char[BUFFER_SIZE]; if(ifs.is_open()) { while (ifs.read(buf, sizeof(buf))) { for(int i = 0; i < sizeof(buf); i++) { cout << buf[i]; } } } ifs.close(); ofs.open(filename.c_str(), ios_base::out | ios_base::app | ios_base::binary); if(!ofs.is_open()) { cout << "file open filed" << endl; exit(EXIT_FAILURE); } string buff; getline(cin, buff, 'n'); ofs.write(buff.c_str(), buff.size()); ofs.close(); ifs.open(filename.c_str(), ios_base::in | ios_base::binary); vector vbuf(BUFFER_SIZE, 0); if(ifs.is_open()) { while (ifs.read((char *)&(vbuf[0]), sizeof(vbuf[0]))) { cout << vbuf[0]; } } ifs.close(); return 0; }



