(C++)输入/输出流

1、输入/输出概念

c++将输入输出分为三类:

标准I/O 文件IO 字符串IO

 //fstream  文件流,使用需要调用库函数:read(); write();等

//ifstream 文件输入流, 将内容存储到文件中;相当于fstream 调用 write();

//ofstream 文件输出流, 将文件内容输出,调入内存;相当于fstream 调用 read();

2、标准输入流

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    char ch;
    char buf[32] = {0};
    cin>>ch;         //获取一个字符
    cout<<ch<<endl;   //打印字符


    ch = cin.get();   //cin>>in 获取一个字符
    cin.get(ch);      //同上


    cin.get(buf,10);  //获取字符串,最多10个字节,放入 buf 中
    cout<<buf<<endl;   //打印出字符串


    cin.getline(buf,10);  //获取一行数据,最多10个字节, 放入 buf 中
    cout<<buf<<endl;      //打印
    
    cin.ignore(5);    //忽略前5个字节
    cin>>buf;         //将忽略掉前五个的字符串,放入 buf
    cout<<buf<<endl;   //打印
    ch = cin.peek();   //获取一个字符,同时把字符留在缓冲区内
    cout<<ch<<endl;
    cin>>buf;           //会将上述在缓冲区中的一个字符连同字符串一起放入buf
    cout<<buf<<endl;   //打印


    cin>>ch;
    cin.putback(ch); //把ch放回缓冲区
    cin>>buf;
    cout<<buf<<endl;   //会将上述在缓冲区中的一个字符连同字符串一起放入buf
    return 0;
}

3、标准输出流

#include <iostream>
#include <iomanip>
using namespace std;

int main(int argc, char const *argv[])
{
#if 0
    int num =1000;
    cout<<oct<<num<<endl;  //八进制输出 
    cout<<dec<<num<<endl;  //十进制
    cout<<hex<<num<<endl;  //十六进制
    cout<<setbase(8)<<num<<endl;   //八进制


    double PI = 222.00000/7.0000;
    cout<<PI<<endl;
    cout<<setprecision(15)<<PI<<endl;                //15位数 含整数与小数部分
    cout<<setprecision(5)<<setiosflags(ios::scientific)<<PI<<endl;  //5位数,以科学计数法输出


    const char *s = "helloworld";
    cout<<setw(15)<<setfill('*')<<s<<endl;    //占15位,右对齐,不够用*补齐
#endif
    int num = 1000;
    cout.unsetf(ios::dec);  //设置10进制格式输出
    cout.setf(ios::oct);  //设置八进制输出格式
    cout<<num<<endl;


    double PI = 222.00000/7.0000;
    cout.precision(5);                 //5位数
    cout<<PI<<endl;
    cout.setf(ios::scientific);        //设置科学计数法
    cout<<PI<<endl;




    const char *s = "helloworld";
    cout.width(15);                    //设置占15位
    cout.fill('^');                    //设置不够,用^补齐
    cout<<s<<endl;
    return 0;
}

4、文件的读写操作

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

int main(int argc, char const *argv[])
{
    
#if 0
    ofstream ofs;                   //创建输出流对象(写文件)
    ofs.open("hello.txt",ios::out);  //使用默认参数,默认是输出格式 ios::out
    ofs<<buf;                      //使用输出运算符把buf写入文件, buf >>ofs 不可以这么使用
    ofs.close();                    //关闭


    char buf[32] = "helloworld";
    memset(buf,0,sizeof(buf));
    ifstream ifs("hello.txt");  //创建输入流对象(读文件),通过构造函数,打开文件
    ifs >> buf;                 //把文件内容读入 buf中
    cout<<buf<<endl;
    ifs.close();
#endif
    char buf[32] = "helloworld";
    //memset(buf,0,sizeof(buf));
    fstream fs("hello.c",ios::out);   //out含有输出属性
    fs.write(buf,strlen(buf));       //将 buf 中的内容写入文件中 fs
    fs.close();


    char ch;
    memset(buf,0,sizeof(buf));
    fs.open("hello.c",ios::in);   //输入(读)的方式打开文件
    //fs.read(buf,sizeof(buf));   //将文件中的内容,读入buf中
    //cout<<buf<<endl;
    while((ch = fs.get()) != EOF)   //end of file
    {
        cout<<ch;                   //从文件中 取一个个字符,逐一输出
    }
    cout<<endl;
    fs.close();
    return 0;
}

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值