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

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/223990

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 下午12:17
下一篇 2023年2月13日 下午12:17

相关推荐