【问题标题】:c++ Problems with pushing structs onto a vector and then saving to an outputfilec++ 将结构推入向量然后保存到输出文件的问题
【发布时间】:2021-04-05 18:06:42
【问题描述】:

我放弃了向量的想法,经过一番折腾后,我设法将结构体放入和取出文件。我真的很享受 C++ 的挑战,要学习的东西太多了!!!

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

struct Account_query
{
public:
    char name[50];
    char number[50];
};

void AddRecord(Account_query* aq)
{
    ofstream fout;
    fout.open("Records.out", ios::app | ios::out | ios::binary);
    cout << "Enter name: ";
    cin >> aq->name;
    cout << "Enter account number: ";
    cin >> aq->number;
    fout.write( reinterpret_cast<const char *>( &(*aq) ), sizeof(Account_query) );
    fout.close();
}
void ShowRecord()
{
    Account_query aq;
    //std::memset(&aq, 0, sizeof(aq));
    ifstream inf;
    inf.open("Records.out", ios::binary);
    if(!inf)
    {
        cout << "Problem opening"<<endl;
    }
    cout <<"####Data Out###"<<endl;
    while(!inf.eof())
    {
        inf.read( reinterpret_cast<char *>(&aq.name), sizeof(Account_query::name));
        inf.read( reinterpret_cast<char *>(&aq.number), sizeof(Account_query::number));
        cout << "Name is: " << aq.name << "Account is: " << aq.number <<endl;
    }
}

int main(int argc, const char * argv[]) {
    
    vector<Account_query> list;
    
    cout << "Welcome to Jizz Bank\n";
    cout << "Enter one of the following options then press enter\n";
    cout << "-----------------------------------\n";
    cout << "1) Add Record \n";
    cout << "2) Show Records \n";
    cout << "3) Search Record \n";
    cout << "4) Edit Record \n";
    cout << "5) Delete Record \n";
    cout << "-----------------------------------\n";
    cout << "Enter your choice: ";
    
    int x;
    cin >>x;
    
    Account_query a;
    Account_query *p = &a;
    
    switch(x)
    {
        case 1:{
            
            AddRecord(p);//got the data for object now
            break;
        }
        case 2:{
            ShowRecord();
            break;
        }
        default:{
            exit(0);
        }
    }
    return 0;
}

下面的先前条目... 我正在尝试编写一个控制台程序来学习 c++。 在开关的情况 1 中,我动态创建一个结构,然后将其连同对向量的引用一起发送到函数。 这个想法是输入一些简单的数据来填充结构,然后将其推送到向量上,然后将其写入文件(二进制)。 问题似乎是:

fout.write(static_cast&lt;char*&gt; (*it), sizeof(Account_query));

我明白了,无法在 Xcode 中将类型“Account_query”转换为指针类型“char *”。 我已经尝试过 reinterpret_cast 等,但这也不起作用。 非常感谢任何帮助, LD。

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

struct Account_query
{
public:
    char name[50];
    char number[50];
};

void AddRecord(Account_query* aq, vector<Account_query>& list)
{
    ofstream fout;
    fout.open("Records.bank", ios::binary | ios::out);
    cout << "Enter name: ";
    cin >> aq->name;
    cout << "Enter account number: ";
    cin >> aq->number;
    list.push_back(*aq);
    
    for (vector<Account_query>::iterator it=list.begin(); it != list.end(); ++it)
    {
        fout.write(static_cast<char*> (*it), sizeof(Account_query));
    }
    fout.close();
}
void ShowRecord()
{
   //TODO
   
}

int main(int argc, const char * argv[]) {
    
    vector<Account_query> list;
    
    cout << "Welcome to Jizz Bank\n";
    cout << "Enter one of the following options then press enter\n";
    cout << "-----------------------------------\n";
    cout << "1) Add Record \n";
    cout << "2) Show Records \n";
    cout << "3) Search Record \n";
    cout << "4) Edit Record \n";
    cout << "5) Delete Record \n";
    cout << "-----------------------------------\n";
    cout << "Enter your choice: ";
    
    int x;
    cin >>x;
    
    switch(x)
    {
        case 1:{
            Account_query* a = new Account_query;
            AddRecord(a, list);//got the data for object now
            break;
        }
        case 2:{
            cout<<"do nowt yet"<<endl;
            break;
        }
        default:{
            exit(0);
        }
    }
    return 0;
}

【问题讨论】:

  • 要将原始数据写入文件,write 函数需要一个 指针 指向要写入的数据的第一个字节。 *itAccount_query 对象,而不是指针。您读过哪些关于指针以及如何获取指向对象的指针的内容?
  • vector&lt;Account_query&gt;::iterator 我认为您正在使用一本非常古老的书或教程...您的来源是什么?
  • 还有一个问题,和指针也有关系……而这个问题可以通过not使用指针来解决。

标签: c++ struct ofstream static-cast


【解决方案1】:

不要让自己的生活变得困难。首先使用std::string,然后使用operator&lt;&lt; 将字符串放入文件中(并从中读取)。例如

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

struct AccountQuery
{
    std::string name;
    std::string number;
};

AccountQuery AddRecord()
{
    AccountQuery aq;
    std::cout << "Enter name: ";
    std::cin >> aq.name;
    std::cout << "Enter account number: ";
    std::cin >> aq.number;
    {
        std::ofstream fout("Records.out", std::ios::app | std::ios::binary);
        fout << aq.name << " " << aq.number << " "; // spaces for simple separation. 
    }
    return aq;
}
void ShowRecord()
{
    AccountQuery aq;
    std::ifstream fin("Records.out", std::ios::binary);
    if(!fin)
    {
        std::cerr << "Problem opening file.\n";
        return;
    }
    std::cout << "####Data Out###\n";
    while(fin >> aq.name && fin >> aq.number) {
        std::cout << "Name is: " << aq.name << ", Account is: " << aq.number << '\n';
    }
}

int main() {
    std::cout
       << "Welcome to Jizz Bank\n"
       << "Enter one of the following options then press enter\n"
       << "-----------------------------------\n"
       << "1) Add Record\n"
       << "2) Show Records\n"
       << "3) Search Record\n"
       << "4) Edit Record\n"
       << "5) Delete Record\n"
       << "6) Exit\n"
       << "-----------------------------------\n";
    
    bool loop = true;
    while(loop) {
        std::cout << "Enter your choice: ";
        int choice;
        std::cin >> choice;
        switch(choice)
        {
            case 1: {
                AddRecord();//return value unused?
                break;
            }
            case 2: {
                ShowRecord();
                break;
            }
            case 6: {
                loop = false;
                break;
            }
            default: {
                // nothing, could be omitted
                break;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2019-04-24
    • 1970-01-01
    相关资源
    最近更新 更多