码迷,mamicode.com
首页 > 编程语言 > 详细

C++ fstream stringstream

时间:2016-08-03 21:58:24      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

一、文件输入输出

C/C++

输入:

freopen("in.cpp", "r", stdin);
fclose(stdin);

输出:

freopen("in.cpp", "r", stdout);
fclose(stdout);

C++

输入:

ifstream cin("in.cpp");

cin.close();

输出:

ofstream cout("out.cpp");

cout.close();

二、istringstream ostringstream 和 stringstream.

常用函数:

string str, s;

stringstream str;

stringstring str(s);

str.clear();

str.str(s);

Eg:

#include <sstream>
#include <iostream>
#include <stdio.h>
#include <fstream>
using namespace std;

string str, name, pnum;
string outstr;

int main() {
    ifstream cin("in.cpp");
    ofstream cout("out.cpp");
    stringstream imess;
    stringstream omess;
    while (getline(cin, str)){
        imess.clear();
        omess.str("");
        imess.str(str);

        imess >> name;
        omess << name << ":";

        while(imess >> pnum) {
            omess << " " << pnum;
        }
        cout << omess.str() << endl;
    }
    cin.close();
    cout.close();
    return 0;
}

 

C++ fstream stringstream

标签:

原文地址:http://www.cnblogs.com/icode-girl/p/5734296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!