首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在C++中将文件读入类?

如何在C++中将文件读入类?
EN

Stack Overflow用户
提问于 2021-04-13 00:00:38
回答 1查看 75关注 0票数 0

我正在读取包含以下内容的文件

代码语言:javascript
复制
Boss,        level,    Specials,
"FireMen",   3,        "Flame,Thrower", Fire,Breath
"Medusa",    6,        "Snake,Poison"
"Hype",      10,       "One,punch,Knock", Fly,Kick, "Dragon,Breath"

我正试着用boss,level和specials对象把它读进班级

我在读取文件时遇到了问题,因为我用逗号将每个单词分开,但由于它们之间的逗号,它将像Flame,Thrower这样的特殊内容作为单独读取。我如何组合特价而不是用逗号拆分,这样它就会读到Flame Thrower而不是Flame和that分开。另外,有些特价商品在报价中,有些则没有。

我一直在阅读这个相当复杂的文件。如果任何人有时间通读我的代码并修复我收到的10个错误,我将非常感激,谢谢

(我所做的没有多大意义,因为这是我进入C++的一个月,所以我还是个新手,还在进步中)

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
#include "Boss.h"

using namespace std;
vector <Boss*> Info;

Boss* parseLine(string str)
{
    vector<string> store;
    string smt = " ";
    Boss* values = nullptr;
    if (smt != "")
    {
        (store.push_back(smt));
    }
    for (int i = 0; i < (int)str.size(); i++)
    {
        char ch = str[i];
        if (ch != ',')
        {
            smt = smt + ch;
        }
        else
        {
            store.push_back(smt);
            smt = "";
        }
    }
    
    values = new Boss(store[0], stoi(store[1]), store[2]);
    Name = store[0];
    Level = store[1];
    Specials = store[2];
    
    return values;
}
bool readFile()
{
    std::ifstream myFile("Bosses.txt");
    if (!myFile.is_open())
    {
        cout << "fAILED" << "\n";
        return false;
    }
    string str;

    int i = 0;
    while (std::getline(myFile, str))
    {
        cout << str << endl;
        if (str[0] != '/')
        {
            Boss* Boss = parseLine(str);
            result.push_back(Boss);
        }
    }
    return true;
}
int main()
{
    std::cout << "Read file\n;";
    bool data = readFile();

    for (Boss* t : result)
{
 delete t;
}

和类

代码语言:javascript
复制
#include <string>
#include <vector>

class Boss {
    std::string Name;
    int Level;
    std::vector<std::string> Specials;
    Boss(std::string Name, int Level, std::vector<std::string> Specials);
    ~Boss();
    Boss(Boss& b);
    void setName(std::string Name);
    void setLevel(int Level);

};
Boss::Boss(std::string Name, int Level, std::vector<std::string> Specials)
{
    this->Name= Name;
    this->Level = Level;
    this->Specials = Specials;
}

Boss::~Boss() {} 

Boss::Boss(Boss& b)
{
    Name = b.Name;
    Level = b.Level;
    Specials = b.Specials;
}
void Boss::setName(std::string Name) {
    this->Name = Name;
}
void Boss::setLevel(int Level)
{
    this->Level = Level;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-13 01:00:16

这段代码读取您的文件,并使用stringstreams和其他要解析的内容将数据存储在std::vector Info中。

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
#include <sstream>


using namespace std;

class Boss {
    std::string Name;
    int Level;
    std::vector<std::string> Specials;
public:
    Boss(std::string n, int l, std::vector<std::string> s)
    {
        this->Name = n;
        this->Level = l;
        this->Specials = s;
    };
    ~Boss() {};
    Boss(const Boss& b) {
        this->Name = b.Name;
        this->Level = b.Level;
        this->Specials = b.Specials;
    };
    Boss() {};

    Boss operator= (Boss b) {
        this->Name = b.Name;
        this->Level = b.Level;
        this->Specials = b.Specials;
        return b;
    }

    void setName(std::string n) { Name = n; };
    void setLevel(int l) { Level = l; };
    void setSpecials(std::vector<std::string> s) {Specials = s;}

    std::string getName() const { return Name; }
    int getLevel() const { return Level; }
    std::vector<std::string> getSpecials() const { return Specials; }
};





vector <Boss> Info;

Boss parseLine(string str)
{
    vector<string> store;
    string smt;
    stringstream evaluator;
    Boss value;
    
    evaluator << str; //put our string into evaluator
    {
        char double_quote_remover;
        evaluator >> double_quote_remover; //remove the first double quote
        getline(evaluator, smt, '"'); //get the name until the next double quote
        char comma_remover;
        evaluator >> comma_remover; //remove the comma
    }
    value.setName(smt); //put smt into the Boss's name
    {
        int Level; 
        evaluator >> Level; //get an int from our string
        value.setLevel(Level); //put our int into our Boss
    }
    char curr;

    while (evaluator >> curr && curr != ','); //get the comma after the int
 
    smt = ""; //reset
    curr = ' '; //reset
 
        while (evaluator >> curr) { //while we read chars into curr
            if (isalpha(curr)) { evaluator.putback(curr); curr = ','; } //if the char is a letter, put it back
            if (curr == '\"') getline(evaluator,smt,'\"'); //if the char is a double quote, read until we reach another
            else if (curr == ',') getline(evaluator,smt,','); //if the char is a comma, read until we get another
            if (smt != "") //if the string is not null, put it in store
                store.push_back(smt);
            smt = ""; //reset
        }

        value.setSpecials(store); //put store into our Boss

        return value; //return our boss
    }

bool readFile()
{
    std::ifstream myFile("Bosses.txt");
    if (!myFile.is_open())
    {
        cout << "FAILED" << "\n";
        return false;
    }
    string str;

    getline(myFile, str); //for the first line, which we don't need

    int i = 0;
    while (std::getline(myFile, str))
    {
        Boss Boss = parseLine(str);
        Info.push_back(Boss);
    }
    return true;
}

int main()
{
    bool data = readFile();
}

这应该是可行的。我已经粗略地测试过了。如果有任何错误,请通知我。

注意:不需要原始代码中的指针。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67061682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档