使用C++上的迭代读取和写入二进制文件

Reading and writing in binary file using iteration on C++

本文关键字:二进制文件 读取 迭代 C++ 使用      更新时间:2023-10-16

我正在做一项涉及将结构保存在二进制文件的系统上的功课,这是使用迭代所必需的(?

我在写作方面都有问题(不确定是否正确(:

void InserirDados () {
    //var created to acknowledge the quantity of items that will be described
    int quantidade;
    cout << "Quantos personagens voce pretende inserir nesta sessao?" << endl;
    cin >> quantidade;
    //allocating memory like it's asked by the teacher
    PersonagemDesenho* objPersonagem = new PersonagemDesenho[quantidade];
    //declaring the flow of data
    ofstream arquivo ("personagens.dat", ios::binary);
    //flush buffer
    cin.ignore ();
    cout << "(A utilizacao de espacos e' permitida para todos os itens a seguir)" << endl;
    //describing items
    for (int i = 0; i < quantidade; i++) {
        cout << " - PERSONAGEM NUMERO: " << i + 1 << endl;
        cout << "Digite o nome do personagem a ser inserido" << endl;
        //getline for getting more than 1 word
        getline(cin, objPersonagem[i].nomePersonagem);
        cout << "Digite o nome do criador do personagem" << endl;
        getline(cin, objPersonagem[i].nomeCriador);
        //writing code
        arquivo.write(reinterpret_cast<const char*> (&objPersonagem[i]), sizeof(PersonagemDesenho));
        }
    cout << "As informacoes serao salvas no arquivo "personagens.dat"" << endl;
    //closing file
    arquivo.close();
}

和读取数据:

void ListaDados () {
    ifstream arquivo ("personagens.dat", ios::binary);
    int i = 0;
    while (???) {
        arquivo.read(reinterpret_cast<const char*> (&objPersonagens[i]) sizeof(PersonagemDesenho))
        i++;
    }
}

从您对std::getline()的使用中可以清楚地看出,nomePersonagemnomeCriadorstd::string值。 std::string 是一种动态类型(它包含指向存储在内存中其他位置的数据的指针(。当结构中包含动态数据时,不能按原样写入/读取结构。您将写入/读取指针本身,而不是指向的数据。

此问题的解决方案需要在写入时将数据序列化为更平坦的格式,并在读取时反序列化数据。

尝试更多类似的东西:

void writeSizeT(ostream &out, size_t value) {
    out.write(reinterpret_cast<char*>(&value), sizeof(value));
}
void writeString(ostream &out, const string &value) {
    size_t len = value.size();
    writeSizeT(out, len);
    out.write(s.c_str(), len);
}
void InserirDados () {
    //var created to acknowledge the quantity of items that will be described
    int quantidade;
    cout << "Quantos personagens voce pretende inserir nesta sessao?" << endl;
    cin >> quantidade;
    //allocating memory like it's asked by the teacher
    PersonagemDesenho* objPersonagem = new PersonagemDesenho[quantidade];
    //declaring the flow of data
    ofstream arquivo ("personagens.dat", ios::binary);
    writeSizeT(arquivo, quantidade);
    //flush buffer
    cin.ignore ();
    cout << "(A utilizacao de espacos e' permitida para todos os itens a seguir)" << endl;
    //describing items
    for (int i = 0; i < quantidade; i++) {
        cout << " - PERSONAGEM NUMERO: " << i + 1 << endl;
        //getline for getting more than 1 word
        cout << "Digite o nome do personagem a ser inserido" << endl;
        getline(cin, objPersonagem[i].nomePersonagem);
        cout << "Digite o nome do criador do personagem" << endl;
        getline(cin, objPersonagem[i].nomeCriador);
        //writing code
        writeString(arquivo, objPersonagem[i].nomePersonagem);
        writeString(arquivo, objPersonagem[i].nomeCriador);
    }
    cout << "As informacoes serao salvas no arquivo "personagens.dat"" << endl;
    //closing file
    arquivo.close();
    // freeing memory
    delete[] objPersonagem;
}

size_t readSizeT(istream &in) {
    size_t value;
    in.read(reinterpret_cast<char*>(&value), sizeof(value));
    return value;
}
string readString(istream &in) {
    string value;
    size_t len = readSizeT(in);
    if (len > 0) {
        value.resize(len);  
        in.read(&s[0], len);
    }
    return value;
}
void ListaDados () {
    ifstream arquivo ("personagens.dat", ios::binary);
    size_t quantidade = readSizeT(arquivo);
    //allocating memory like it's asked by the teacher
    PersonagemDesenho* objPersonagem = new PersonagemDesenho[quantidade];
    for(size_t i = 0; i < quantidade; ++i) {
        //reading code
        objPersonagem[i].nomePersonagem = readString(arquivo);
        objPersonagem[i].nomeCriador = readString(arquivo);
    }
    // freeing memory
    delete[] objPersonagem;
    //closing file
    arquivo.close();
}