【发布时间】: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<char*> (*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函数需要一个 指针 指向要写入的数据的第一个字节。*it是Account_query对象,而不是指针。您读过哪些关于指针以及如何获取指向对象的指针的内容? -
vector<Account_query>::iterator我认为您正在使用一本非常古老的书或教程...您的来源是什么? -
还有一个问题,和指针也有关系……而这个问题可以通过not使用指针来解决。
标签: c++ struct ofstream static-cast