streaminherit.cpp

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 41 行

CPP
41
字号
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
#include "prompt.h"

// show stream inheritance

void doInput(istream& input)
// precondition: there are three strings to read in input
{
    string s;
    int k;
    for(k=0; k < 3; k++)
    {   input >> s;
        cout << k << ".\t" << s << endl;
    }
    cout << endl;
}

int main()
{
    string filename = PromptString("filename: ");
    ifstream input(filename.c_str());
    string firstline;
    getline(input,firstline);            // first line of input file
    istringstream linestream(firstline); // stream bound to first line
    
    cout << "first three words on first line are\n-----" << endl;
    doInput(linestream);
    
    cout << "first three words on second line are\n---" << endl;
    doInput(input);
    
    cout << "first three words from keyboard are\n---" << endl;
    doInput(cin);
    return 0;
}
    

⌨️ 快捷键说明

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