streams.cpp

来自「压缩包里有教材<<C++模式设计-基于QT4开源跨平台开发框架&gt」· C++ 代码 · 共 60 行

CPP
60
字号
//start id="writing"#include <iostream>#include <sstream>#include <fstream>int main() {    using namespace std;    ostringstream strbuf;    int lucky = 7;    float pi=3.14;    double e=2.71;        /* An in-memory stream */    strbuf << "luckynumber " << lucky << endl           << "pi " << pi << endl           << "e " << e << endl;    string strval = strbuf.str(); /* Convert the stringstream to a string. */    cout << strval;    /* An output file stream. */    ofstream outf;     outf.open("mydata");   /* Creates (or overwrites) a disk file for output. */    outf << strval ;       outf.close();//end//start id="reading"    /* An input file stream */    ifstream inf;       inf.open("mydata");     string newstr;      int lucky2;    inf >> newstr >> lucky2;    if (lucky != lucky2)        cerr << "ERROR! wrong lucky number" << endl;        float pi2;    inf >> newstr >> pi2;    if (pi2 != pi) cerr << "ERROR! Wrong pi." << endl;        double e2;    inf >> newstr >> e2;    if (e2 != e) cerr << "e2: " << e2 << " e: " << e << endl;    inf.close();   //end//start id="getline"    /* Read line-by-line */    inf.open("mydata");    while (not inf.eof()) {        getline(inf, newstr);        cout << newstr << endl;    }    inf.close();    return 0;}//end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?