栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

STL:输入输出类模板

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

STL:输入输出类模板

STL:输入输出类模板

参考文献《大道至简:C++STL》

  • 简单操作
#include 
#include 

using namespace std;

void scicout(const double& x){
    cout.scientific;
    cout.precision(6);
    cout << x << endl;
}

int main(){



    double d = 12345678.5678901234;
    scicout(d);     //1.23457e+07



#if false
    ios::iostate olde = cin.exceptions();
    cout << "Old exceptions: " << olde << endl;
    int x = 0;
    try{
        cin.exceptions(ios::failbit | ios::eofbit | ios::badbit);
        cin >> x;
    }
    catch(const std::exception& e){
        std::cerr << e.what() << 'n';
    }
#endif



#if false
    char chdim[] = {0, 0, 0, 0, 0, 0, 0};
    cin.get(chdim, 6, 'n');
    cout.write(chdim, 6);
    cout << endl;
    cin.get();

    cin.getline(chdim, 6, 'n');
    cout.write(chdim, 6);
    cout << endl;
    cin.get();
#endif

    return 0;
}
  • 缓冲区迭代器
#include 
#include 
#include 

using namespace std;

int main(){



    string str("VT&y8V& tHUBUy .n");
    ostreambuf_iterator bufwrite(cout);
    copy(str.begin(), str.end(), bufwrite);

    istreambuf_iterator inpos(cin);
    istreambuf_iterator endpos;
    ostreambuf_iterator outpos(cout);
    while (inpos != endpos){
        *outpos = *inpos;
        inpos++;
        outpos++;
    }

    return 0;
}
  • 自定义缓冲区
#include 
#include 
#include 

using namespace std;

class outbuf : public streambuf
{
public:
    virtual int_type overflow(int_type c)
    {
        if(c != EOF)
        {
            c = toupper(c, getloc());
            if(putchar(c) == EOF)
            {
                return EOF;
            }
        }
        return c;
    }
};

int main()
{
    outbuf ob;
    ostream out(&ob);
    int num = 56;
    out << hex << showbase << num << endl;
    return 0;
}
  • 文件流
#include 
#include 
#include 

using namespace std;

int main()
{

    string filename;
    cin >> filename;
    ofstream fout(filename.c_str());
    fout << "Rand Number : " << rand() << endl;;
    fout.close();

    ifstream fin(filename.c_str());
    char ch;
    while (fin.get(ch))
    {
        cout << ch;
    }
    fin.close();

    ifstream fin2(filename.c_str());
    cout << fin2.rdbuf();
    fin2.close();

    return 0;
}
  • 文件读写
#include 
#include 
#include 

using namespace std;

const string filename = "test";

int main()
{
    ifstream fin;
    fin.open(filename.c_str(), ios_base::in);
    if(fin.is_open())
    {
        cout << fin.rdbuf();
    }
    fin.close();

    ofstream fout;
    fout.open(filename.c_str(), ios_base::out | ios_base::app);
    if(!fout.is_open())
    {
        cout << "file open filed" << endl;
        exit(EXIT_FAILURE);
    }
    string buffer;
    while(getline(cin, buffer) && buffer.size())
    {
        fout << buffer << endl;
    }
    fout.close();
    
    fin.open(filename.c_str(), ios_base::in);
    if(fin.is_open())
    {
        cout << fin.rdbuf();
    }
    fin.close();
    
    return 0;
}
  • 二进制文件读写
#include 
#include 
#include 
#include 
#include 

#define BUFFER_SIZE 256

using namespace std;

const string filename = "log.dat";

int main()
{
    ifstream ifs;
    ofstream ofs;

    ifs.open(filename.c_str(), ios_base::in | ios_base::binary);
    char *buf = new char[BUFFER_SIZE];
    if(ifs.is_open())
    {
        while (ifs.read(buf, sizeof(buf)))
        {
            for(int i = 0; i < sizeof(buf); i++)
            {
                cout << buf[i];
            }
        }
    }
    ifs.close();

    ofs.open(filename.c_str(), ios_base::out | ios_base::app | ios_base::binary);
    if(!ofs.is_open())
    {
        cout << "file open filed" << endl;
        exit(EXIT_FAILURE);
    }
    string buff;
    getline(cin, buff, 'n');
    ofs.write(buff.c_str(), buff.size());
    ofs.close();

    ifs.open(filename.c_str(), ios_base::in | ios_base::binary);
    vector vbuf(BUFFER_SIZE, 0);
    if(ifs.is_open())
    {
        while (ifs.read((char *)&(vbuf[0]), sizeof(vbuf[0])))
        {
            cout << vbuf[0];
        }
    }
    ifs.close();

    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/396151.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号