gpt4 book ai didi

c++ - 创建一个二进制文件,向其添加信息,并从中读取所有记录(电话簿)

转载 作者:行者123 更新时间:2023-11-28 03:42:04 27 4
gpt4 key购买 nike

感谢您的大力支持。我得承认 C++ 很有趣而且非常了不起,但有时它会让人头疼。我一直在努力解决这个家庭作业。它快完成了,但我在尝试弄清楚如何使用二进制文件时遇到了一些困难。

问题如下:

You have been hired to help program a new "Ma Bell" telephone feature. The feature allows users with "PC" to obtain a disk of telephone numbers in addition to a telephone book. Your job is to write the program which will allow the phone information to be used. You must allow the addition of and viewing of phone number information.

The program should be able to: 1. Add people to the telephone list 2. View all people in the list 3. View based on just the last name

You may make the following assumptions:

1. There are no more than 100 phone numbers
2. No name is longer than 50 characters.
3. Only one telephone number per person.
4. The address should be broken into only
street # or PO Box
street name
zip code

我写的代码如下:

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

class phonebook
{
private: char fname[25];
char lname[25];
char tel[10];
int pobox;
char street[20];
int zipcode;
int id;

public: phonebook(char[]=" ",char[]=" ",char[]=" ",int=0,char[]=" ",int=0,int=0);
void setfname(char*);
void setlname(char*);
void settel(char*);
char* getfname();
char* getlname();
char* gettel();
void setpobox(int);
void setstreet(char*);
void setzipcode(int);
int getpobox();
char* getstreet();
int getzipcode();
void setid(int);
int getid();
};

phonebook::phonebook(char* fn,char* ln, char* t,int p,char* s, int z,int i)
{
setfname(fn);
setlname(ln);
settel(t);
setpobox(p);
setstreet(s);
setzipcode(z);
setid(i);
}
void phonebook::setfname(char* fn){strcpy(fname,fn);}
void phonebook::setlname(char* ln){strcpy(lname,ln);}
void phonebook::settel(char* t){strcpy(tel,t);}
char* phonebook::getfname(){return fname;}
char* phonebook::getlname(){return lname;}
char* phonebook::gettel(){return tel;}
void phonebook::setpobox(int p){pobox=p;}
void phonebook::setstreet(char* s){strcpy(street,s);}
void phonebook::setzipcode(int z){zipcode=z;}
int phonebook::getpobox(){return pobox;}
char* phonebook::getstreet(){return street;}
int phonebook::getzipcode(){return zipcode;}
void phonebook::setid(int i){id=i;}
int phonebook::getid(){return id;}

int main()
{
phonebook pb;
fstream outfile("phonebook.dat",ios::in | ios::binary);
if(!outfile){cerr<<"file could not be created!";exit(1);}

//for(int i=0;i<100;i++)
//outfile.write(reinterpret_cast<const char*>(&pb),sizeof(phonebook));


char FN[25];
char LN[25];
char T[10];
int P;
char S[20];
int Z;
int x;
int id;
cout<<"Choose one of the following: "<<endl;
cout<<"1. Add people to the telephone list"<<endl;
cout<<"2. View all people in the list"<<endl;
cout<<"3. View based on just the last name"<<endl;
cin>>x;

switch(x)
{
case 1:
cout<<"Enter record number, first name, last name, telephone, pobox, street, zipcode: "<<endl;
cin>>id>>FN>>LN>>T>>P>>S>>Z;
pb.setid(id);
pb.setfname(FN);
pb.setlname(LN);
pb.settel(T);
pb.setpobox(P);
pb.setstreet(S);
pb.setzipcode(Z);
outfile.seekp((id-1)*sizeof(phonebook));
outfile.write(reinterpret_cast<const char*>(&pb),sizeof(pb));break;

case 2:
for(int i=0;i<100;i++){
outfile.seekg((i-1)*sizeof(phonebook));
cout<<pb.getfname()<<" "<<pb.getlname()<<" "<<pb.gettel()<<" "<<pb.getpobox()<<" "<<pb.getstreet()<<" "<<pb.getzipcode()<<endl;}break;

case 3:

cout<<"Enter last name: ";
cin>>LN;
outfile.seekp((id-1)*sizeof(phonebook));
outfile.read(reinterpret_cast<char*>(&pb),sizeof(pb));
if((strcmp(pb.getlname(),LN)==0))
cout<<pb.getfname()<<" "<<pb.getlname()<<" "<<pb.gettel()<<" "<<pb.getpobox()<<" "<<pb.getstreet()<<" "<<pb.getzipcode()<<endl;
else cout<<"name not found!"<<endl;break;
}



system("pause");
return 0;
}

当我想列出我添加到二进制文件的信息时,它没有读取。我只得到零。我也想知道如何制作 switch 语句来询问我是否要添加更多电话号码或类似 switch 案例的循环。另外,如何让程序在二进制文件中搜索名称。我尽了最大努力,但就是做不到。

最佳答案

你应该验证你的文件操作是否成功!提示:如果你真的想要一个写操作成功的机会,你可能要考虑打开文件写模式。就目前而言,如果您设法写入输入文件,我会感到惊讶。

关于c++ - 创建一个二进制文件,向其添加信息,并从中读取所有记录(电话簿),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8858555/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com