【C++知识】IO类

前言

       这一章介绍了关于C++的IO类,关于文件的读入和输出。

【C++系列】【前一章:类】【后一章:顺序容器】

五、IO类

IO对象无拷贝或赋值

ofstream out1,out2;
out1=out2;  //赋值错误
ofstream print(ofstream);   //不能初始化ofstream参数
out2=print(out2);          //不能拷贝流对象

文件的读入和输出

#include<iostream>
#include<fstream>
#include <string>
using namespace std;

int main
{
    ifstream in;
    in.open("F:\\C++\\testC++\\main.txt");

    //逐行读入
    string out;
    while (getline(in, out))
    {
	cout << out << endl;
    }

    //逐字符读入,忽略空格和回车
    char c;
    while (!in.eof())
    {
	in >> c;
	cout << c << endl;
    }

    //逐字符读入,包括空格和回车
    char ch;
    in >> noskipws;  
    while (!in.eof())
    {
	in >> ch;
	cout << ch << endl;
    }

    return 0;
}

string流

istringstream从string读取数据,ostringstream向string写入数据,头文件要加上#include<sstream>

istringstream in(str);    //in即可像cin一样用in>>word把str一行的字符串逐个读出每个单词到word中,即遇到空格停止

ostringstream out;    //out即可像cout一样用out<<word把word读入到out中,用cout<<out.str()即可输出读入的数据

上一篇: 【C++知识】类
下一篇: 【C++知识】顺序容器
烊萌
博客等级 码龄10年 537粉丝 161原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烊萌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值