• C++-IO库---istringtream(包含大小端测试)


    #include <iostream>
    #include<sstream>
    #include <vector>
    #include <stdexcept>
    using namespace std;
    istream &f(istream &in)
    {
        string v;
        while (in >> v, !in.eof())
        {
            if (in.bad())
                throw runtime_error("IO流错误");
            if (in.fail())
            {
                cerr << "数据错误" << endl;
                in.clear();
                in.ignore(100, '
    ');
                continue;
            }
            cout << v << endl;
    
        }
        in.clear();
        return in;
    }
    int main()
    {
        ostringstream msg;
        msg << "C++ PRIMER" << endl;
        istringstream in(msg.str());
        f(in);
    
        system("pause");
        return 0;
    }
    
    ////////////////////////////////////////////////////////////////////////
    #include <stdlib.h>
    #include <stdio.h>
    int main(int argc, char **argv)
    {
        union
        {
            short s;
            char c[sizeof(short)];
        } un;
        un.s = 0x0102;      //258十六进制位是每两位为一个字节
        if (sizeof(short) == 2)
        {
            if (un.c[0] == 1 && un.c[1] == 2)
                printf("big-endian
    ");
            else if (un.c[0] == 2 && un.c[1] == 1)
                printf("little-endian
    ");
            else
                printf("unknown
    ");
        }
        else
            printf("sizeof(short)= %d
    ", sizeof(short));
        system("pause");
        exit(0);
    }               //笔记本电脑intercore是小端,其实就是低尾端;
    
    ////////////////////////////////////////////////////////////////////////
    #include <iostream>
    #include<fstream>
    #include<sstream>
    #include <vector>
    #include<string>
    using namespace std;
    int main(int argc, char **argv)
    {
        ifstream in("data");
        if (!in)
        {
            cerr << "we cant open the file" << endl;
            return -1;
    
        }
        string line;
        vector<string>words;
        while (getline(in, line))
        {
            words.push_back(line);
        }
        in.close();
        vector <string>::const_iterator it = words.begin();
        while (it != words.end())
        {
            istringstream line_str(*it);
            string word;
            while (line_str >> word)
                cout << word << "  ";
            cout << "  " << endl;
            ++it;
        }
    
    }
    
    //////////////////////////////////////////////////////////////////////////
    #include <iostream>
    #include<vector>
    #include<string>
    #include<sstream>
    #include<fstream>
    using namespace std;
    struct PersonInfo 
    {
        string name;
        vector<string>phones;
    };
    string format(const string &s) { return s; }
    bool valid(const string &s) { return true; }
    int main(int argc, char *argv[])
    {
        string line, word;
        vector<PersonInfo>people;
        istringstream record;
        if (argc != 2);
        {
        cerr << "请给出文件名" << endl;
        system("pause");
        return -1;
        }
        ifstream in("data");
        if (!in)
        {
            cerr << "文件打不开" << endl;
            return -1;
        }
        while (getline(in,line))
        {
            PersonInfo info;
            record.clear();
            record.str(line);
            record >> info.name;
            while (record >> word)
                info.phones.push_back(word);
            people.push_back(info);
        }
        ostringstream os;
        for (const auto &entrry : people)
        {
            ostringstream formatted, badNums;
            for (const auto &nums : entrry.phones)
            {
                if (!valid(nums))
                {
                    badNums << " " << nums;
                }
                else
                {
                    formatted << " " << format(nums);
    
                }
            }
            if (badNums.str().empty())
                os << entrry.name << " " << formatted.str() << endl;
            else
                cerr << entrry.name << " " << formatted.str() << endl;
    
    
        }
        cout << os.str() << endl;
        system("pause");
        return 0;
    }
  • 相关阅读:
    《机器与人》(读至第一章)
    Solution Architect
    InfoQ Trends Report
    阿里云场景化阿里云企业数字化转型售前方法PSA
    AWS 6R
    .NET Core Ecosystem
    《如何阅读一本书》
    《数字化转型方法论:落地路径与数据中台》
    《亿级流量网站架构核心技术》
    .NET面试题整理
  • 原文地址:https://www.cnblogs.com/VCctor/p/5100697.html
Copyright © 2020-2023  润新知