C++ 类,与不同用户一起处理一个文件

标签 c++ ifstream file-handling

我有一个基类和一个派生类。目前,我正在分别读取每个文件,即 carfile 和 sportsCarFile。然后将它们加载为成员以用于其他目的。我试图了解如何读取一个文件,然后正确读取文件中的每一行并调用正确的加载函数。下表显示了数据的外观:

Car/SportsCar  CarName Age Colour Price 
Car            Abbort  8   Yellow 899.99
SportsCar      Aufdi   7   Brown  989.99       
Car            ATX     5   White  9823.23
Car            POL     3   Yellow 8232.33   

这是当前代码:

#include "stdafx.h"

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

using namespace std;

class Car{
public:
    Car();
    virtual void Load(ifstream& carFile);
    virtual int LoadString(string filename);
    void display();

protected:
    string CarName;
    int Age;
    string Colour;
    double Price;
    int countCars;
    Car *ptrToCarList;
};

class SportsCar : public Car
{
public: 
    SportsCar();
    virtual void LoadSports(ifstream& carFile);
    virtual int LoadString(string filename);
    void displayLoad();
    void display();
protected:
    int engineSize;
    SportsCar *ptrToSportsCarList;
    int countCars;

};

Car::Car()
{
    CarName = "Unknown";
    countCars = 0;
}

void Car::Load(ifstream& carFile)
{
    carFile >> CarName >> Age >> Colour >> Price;
}

int Car::LoadString(string filename)
{
    ifstream inFile(filename);

    if (!inFile)
    {
        cout << "Sorry, file not found" << endl;
        return -1;
    }

    ptrToCarList = new Car[countCars];

    for (int i = 0; i < countCars; i++)
    {
        ptrToCarList[i].Load(inFile);
    }
    inFile.close();
    return 0;
}

void Car::display()
{
    cout << CarName << " " << Age << " " << Colour << " " << Price << " " ;
}

void SportsCar::displayLoad() 
{
    Car::display();
    cout<<engineSize<<endl;
}

void SportsCar::display()
{
    for (int i = 0; i < countCars; i++)
    {
        ptrToSportsCarList[i].displayLoad();
    }
}


void SportsCar::LoadSports(ifstream& carFile){
    Car::Load( carFile);
    carFile >> engineSize;
}

SportsCar::SportsCar() 
{
    CarName = "Unknown";
    countCars = 0;

}

int SportsCar::LoadString(string filename)
{
    ifstream inFile(filename);

    if (!inFile)
    {
        cout << "Sorry, file not found" << endl;
        return -1;
    }
    countCars = 2;

    ptrToSportsCarList = new SportsCar[countCars];

    for (int i = 0; i < countCars; i++)
    {
         ptrToSportsCarList[i].LoadSports(inFile);
    }
    inFile.close();
    return 0;
}

int main()
{
    SportsCar example2;
    example2.LoadString("sportsCarFile.txt");
    example2.display();
    return 0;
}

最佳答案

我会创建一个函数来读取汽车然后返回适当的类型(使用 shared_ptr 所以我们不需要管理内存):

shared_ptr<Car> loadCar(ifstream& in)
{
    string type;
    in >> type; // is this syntax right? I don't use C++ very often
    shared_ptr<Car> car;
    if (type == "SportsCar") {
        car = new SportsCar();
    } else if (type == "Car") {
        car = new Car();
    } else {
        throw [some sort of exception?];
    }
    car.load(in);
}

我觉得这叫工厂。好处是你的Car - 解析代码会弄清楚它是什么类型的汽车,所以这是你在使用 API 时不必考虑的事情:

ifstream f(filename);
vector<shared_ptr<Car>> cars;
while (!f.eof()) {
    cars.push_back(loadCar(f));
}

请注意 Car::load()SportsCar::load()应该都返回一辆汽车,而不是像您当前的 Car 那样的列表类(class)。我选择使用 vector 因为他们很容易合作。您的类(class)也过于复杂。

所以,我会把你的类(class)改成这样:

class Car{
public:
    Car();
    virtual ~Car();
    virtual void load(ifstream& carFile);
    virtual void display();

protected:
    string name;
    int age;
    string colour;
    double price;
};

class SportsCar : public Car
{
public: 
    SportsCar();
    virtual void load(ifstream& carFile);
    virtual void display();
protected:
    int engineSize;
};

或者更好,overload operator<<() , 所以你可以做 cout << car .

关于C++ 类,与不同用户一起处理一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29709482/

相关文章:

c++ - 在 char 指针的末尾追加一个字符

c++ - linux C++ xlib,处理器速度快吗?

c++ - 处理不正确的参数值

C++ ios :fail() flag

c++ - 使用 istream/ostream 传输消息的类似网络协议(protocol)

C++使用ifstream从dat文件加载数据

捕获正在写入打开文件描述符的数据

file - 打开未正确关闭的文件会怎样?

c - 使用C语言写入文本文件

C++11 垃圾收集器 - 为什么和如何