IO库

1、IO库类型和头文件
头文件:iostream fstream sstream
iostream: istream、ostream、iostream
fstream: ifstream ofstream fstream
sstream: istringstream ostringstream stringstream
以上都是操纵char类型数据,对于宽字符语言(wchar_t),还有一组输入输出类来处理此类型。即在原IO类型上加w。如:wistream、wostream。对应的还有 wcin、wcout、wcerr。
2、IO对象不能拷贝、赋值。因此,io对象不能直接作为函数参数和返回值。
3、while检测流的状态:
int a;
while(cin>>a){};
4、刷新缓冲区:
endl:输出换行,刷新缓冲区。
ends: 输出空格,刷新缓冲区。
flush:刷新缓冲区
5、unitbuf:
cout>>unitbuf; //所有的输出都会刷新缓冲区
cout>>nounitbuf; //回到正常缓冲方式
6、输入输出流可以相互关联。
cin.tie(&cout);
7、istream& 可以接收ifstream对象。iostream& 可以接受fstream对象。
在fstream中,有open()、close() 操作来打开,关闭文件。
8、读取文件:

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include<fstream>
#include <string>
#include<vector>
#include<map>
#include<set>

using namespace std;

int main()
{
    ifstream ifs;
    char buffer[1024];
    ifs.open("C:\\Users\\fm\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication1\\f.txt");
    if (ifs.is_open())
    {
        cout << "文件打开"<<endl;
    }
    while (!ifs.eof())
    {
        ifs.getline(buffer,100);
        cout << buffer<<endl;

    }
    ifs.close();

    return 0;
}

getline有两种:一个是string类中的,另一个在ifstream中。
(1)ifstream中:
getline(char *s,streamsize n); //读取一行。
getline最多读取n个字符保存在s对应的数组中,若遇到\n本次读取结束,\n不会读到数组中。
string key;
while(ifs>>key){} //读取每个单词
(2)string中:
getline(istream&,string)
读取一行到string中,不读取\n。
(3)while (ifs>>key&&getline(ifs,value)) //每一行的第一个单词读到key中,剩余部分读到value中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值