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, '\n');
            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\n");
        else if (un.c[0] == 2 && un.c[1] == 1)
            printf("little-endian\n");
        else
            printf("unknown\n");
    }
    else
        printf("sizeof(short)= %d\n", 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;
}

转载于:https://www.cnblogs.com/VCctor/p/5100697.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值