我的ifstream方法有什么问题?

时间:2015-12-19 21:29:18

标签: c++ file oop ifstream

在我班上我有这个ifstream方法 我需要它来读取文件并在我已经创建的对象中写入信息 代码有问题 首先,在值“ - 汽油”

之前插入一个空格的a.engiene值

当第二个对象和第三个对象被赋值时,该方法不会为每个属性分配正确的值。

 friend ifstream& operator>>(ifstream& in, Auto &a)
        {
            char temp[31];
            temp[0] = '\0';
            in.getline(temp, 30, ':');

            if (temp[0])
            {
                in.getline(temp, 30, ':');
                delete[]a.engine;
                a.engine = new char[strlen(temp) + 1];
                strcpy(a.engine, temp);

                in.getline(temp, 30, ':');
                a.max_speed = atoi(temp);

                in.getline(temp, 30, ':');
                a.engine_cc = atoi(temp);

                in.getline(temp, 30, ':');
                a.avg_consumption_urban = atoi(temp);

                in.getline(temp, 30, ':');
                a.avg_speed_urban = atoi(temp);

                in.getline(temp, 30, ':');
                a.avg_consumption = atoi(temp);

                in.getline(temp, 30, ':');
                a.avg_speed = atoi(temp);

                return in;
            }
            else return in;

这就是我在main中调用方法的方法:

        ifstream f1("autoc.txt", ios_base::in);
                f1 >> auto1 >> auto2 >> auto3;

这是文件数据:

    auto1
    engine: gasoline
    max_speed: 250
    engine_cc: 1980
    avg_consumption_urban: 11
    avg_speed_urban: 50
    avg_consumption: 8
    avg_speed: 100
    auto2
    engine: diesel
    max_speed: 230
    engine_cc: 1600
    avg_consumption_urban: 9
    avg_speed_urban: 50
    avg_consumption: 6
    avg_speed: 80
    auto3
    engine: hybrid
    max_speed: 190
    engine_cc: 1450
    avg_consumption_urban: 7
    avg_speed_urban: 50
    avg_consumption: 4
    avg_speed: 90

这是输出窗口: http://imgur.com/tHe49se

这与我的其他问题不重复。 我有这个代码几乎可以工作。我需要让它为每个对象的属性分配正确的值。

2 个答案:

答案 0 :(得分:0)

in.getline(temp, 30, ':');

读取:,它们是属性名称。您还希望在此之后提取值,因此您需要添加

in.getline(temp, 30);
每次

in.getline(temp, 30, ':');

但请记住,您的程序完全忽略了属性名称,只是按值的顺序排列。我希望这能一劳永逸地解决你的问题。

我害怕添加一个更简洁的版本(你可能会再次询问),所以我只会提到它(就像我在原始问题的答案中所做的那样)。这是关于使用std::string temp;std::getline而不是std::istream::getline

答案 1 :(得分:0)

我相信你可以将大部分解析委托给STL本身。您的" autos"的格式似乎是固定的并且有一个固定的顺序,这大大简化了解析的方法(你是否坚持使用" char [31]"目前还不清楚,但如果你不是你,那么很明显最好使用std :: string,因为" name"字段是我添加到Auto结构中的情况)

工作样本:

#include <fstream>
#include <locale>
#include <iostream>
#include <string>


struct SeparatorReader: std::ctype<char>
{
    template<typename T>
    SeparatorReader(T &&seps):
        std::ctype<char>(get_table(seps), true) {}

    template<typename T>
    std::ctype_base::mask const *get_table(T &&seps) {
        auto rc = new std::ctype_base::mask[std::ctype<char>::table_size]();
        for(auto &sep: seps)
            rc[static_cast<unsigned char>(sep)] = std::ctype_base::space;
        return &rc[0];
    }
};


struct Auto
{
    std::string name;
    char engine[31];
    int max_speed;
    int engine_cc;
    int avg_consumption_urban;
    int avg_speed_urban;
    int avg_consumption;
    int avg_speed;

    Auto(const std::string &name) : name(name) {}

    friend std::istream &operator >>(std::istream &is, Auto &a);
    friend std::ostream &operator <<(std::ostream &os, const Auto &a);
};

std::istream &operator >>(std::istream &is, Auto &a)
{
    char tmp[31] = "";

    is >> tmp; is >> a.engine; // skip field name, read value
    is >> tmp; is >> a.max_speed;
    is >> tmp; is >> a.engine_cc;
    is >> tmp; is >> a.avg_consumption_urban;
    is >> tmp; is >> a.avg_speed_urban;
    is >> tmp; is >> a.avg_consumption;
    is >> tmp; is >> a.avg_speed;

    return is;
}

std::ostream &operator <<(std::ostream &os, const Auto &a)
{
    os << a.name << std::endl;
    os << "engine: " << a.engine << std::endl;
    os << "max_speed: " << a.max_speed << std::endl;
    os << "engine_cc: " << a.engine_cc << std::endl;
    os << "avg_consumption_urban: " << a.avg_consumption_urban << std::endl;
    os << "avg_speed_urban: " << a.avg_speed_urban << std::endl;
    os << "avg_consumption: " << a.avg_consumption << std::endl;
    os << "avg_speed: " << a.avg_speed << std::endl;

    return os;
}

int
main(int argc, char *argv[])
{
    std::ifstream stream(argv[1]);
    stream.imbue(std::locale(stream.getloc(), new SeparatorReader(" :\n")));

    std::string name;
    while(stream >> name) {
        Auto a(name);
        stream >> a;

        std::cout << "------------------------------" << std::endl;
        std::cout << a;
    }
}

如果我们使用您的示例输入传递示例文件...输出为:

------------------------------
auto1
engine: gasoline
max_speed: 250
engine_cc: 1980
avg_consumption_urban: 11
avg_speed_urban: 50
avg_consumption: 8
avg_speed: 100
------------------------------
auto2
engine: diesel
max_speed: 230
engine_cc: 1600
avg_consumption_urban: 9
avg_speed_urban: 50
avg_consumption: 6
avg_speed: 80
------------------------------
auto3
engine: hybrid
max_speed: 190
engine_cc: 1450
avg_consumption_urban: 7
avg_speed_urban: 50
avg_consumption: 4
avg_speed: 90