如何在C ++中解析此文件中的空格和换行符? [英] How to parse in this file in c++ dealing with spaces and new lines?

查看:48
本文介绍了如何在C ++中解析此文件中的空格和换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Credit 1 2 150 12345678 10-10-2020 123
Cash 2 3 199 200 1
Check 1 3 100 111000614 124356499
Credit 2 1 50 987654321 10-10-2021 321

我正在尝试用c ++读取此文件,但我不知道执行此操作的正确方法.我需要将每个单独的数据点读入不同的向量.例如,在称为交易类型的向量中,将为信用现金支票Credit .

I am trying to read in this file in c++ but I can't figure out a proper way to do it. I need to read in each individual data point into different vectors. For example in a vector called transaction type would be Credit cash check Credit.

这是我现在拥有的代码,它给了我非常奇怪的结果

This is the code I have now and it gives me really weird results

    file.open(fileName);
    string line;
    string space;
    while(getline(file,line,'\n')){
        cout<<"Row: ";
        while(getline(file,space,' ')){
            cout<<space<<" ";
        }
    }
    cout<<space<<" ";
    return 0;```

推荐答案

在C ++中,空格和换行符被分类为(空白),请参见 std :: isspace . std :: istream&运算符>>(std :: istream& ;, T&) 跳过所有空格,因此不需要对空格或换行符进行特殊处理.

Spaces and newlines are classified as (white)space in C++, see std::isspace. std::istream& operator>>(std::istream&, T&) skips all whitespace, so that no special handling for spaces or newlines is required.

一个例子:

#include <iostream>
#include <string>
#include <vector>

struct Date {
    unsigned yyyymmdd;
};

std::istream& operator>>(std::istream& s, Date& date) {
    std::string t;
    s >> t;
    int dd = std::stoi(t.substr(0, 2));
    int mm = std::stoi(t.substr(3, 2));
    int yyyy = std::stoi(t.substr(6, 4));
    date.yyyymmdd = yyyy * 10000 + mm * 100 + dd;
    return s;
}

struct Credit {
    int a, b, c, d, f;
    Date e;
};

std::istream& operator>>(std::istream& s, Credit& c) {
    return s >> c.a >> c.b >> c.c >> c.d >> c.e >> c.f;
}

struct Cash {
    int a, b, c, d, e;
};

std::istream& operator>>(std::istream& s, Cash& c) {
    return s >> c.a >> c.b >> c.c >> c.d >> c.e;
}

struct Check {
    int a, b, c, d, e;
};

std::istream& operator>>(std::istream& s, Check& c) {
    return s >> c.a >> c.b >> c.c >> c.d >> c.e;
}

template<class V>
void load_element(V& v, std::istream& s) {
    v.emplace_back();
    s >> v.back();
}

struct Data {
    std::vector<Credit> credits;
    std::vector<Cash> cashs;
    std::vector<Check> checks;

    Data(std::istream& s) {
        for(std::string type; s >> type;) {
            if(type == "Credit")
                load_element(credits, s);
            else if(type == "Cash")
                load_element(cashs, s);
            else if(type == "Check")
                load_element(checks, s);
            else
                throw;
        }
    }
};

int main() {
    std::istream& file = std::cin;
    Data d(file);
    std::cout << "credits: " << d.credits.size() << '\n';
    std::cout << "cashs: " << d.cashs.size() << '\n';
    std::cout << "checks: " << d.checks.size() << '\n';
}

以以下方式运行:

./test < input.txt

input.txt 包含帖子中的4条输入行.

input.txt contains the 4 input lines from your post.

输出:

credits: 2
cashs: 1
checks: 1


现金用英语无法计数,但在编程时喜欢区分标量和向量/容器,因此后缀是现金的向量.我的一些同事甚至对所有容器都使用后缀 s ,而不管英语的任意不规则规则,例如标量为 currency ,矢量为 currency .


Cash in uncountable in English, however, in programming one likes to distinguish scalars and vectors/containers, hence s suffix for a vector of cash. Some of my colleagues go as far as just using suffix s for all containers regardless of arbitrary irregular rules of English, e.g. currency for scalars and currencys for vectors.

这篇关于如何在C ++中解析此文件中的空格和换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆