English 中文(简体)
创建双向档案,为之增添新内容,并阅读所有记录(电话簿)。
原标题:creating a binary file, adding info to it, and reading all records from it (phonebook)

我要感谢您的大力支持。 i gotta承认,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

您可提出以下假设:

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;
}

when i want to list the info i added to the binary file, its not reading. I m only getting zeros. also i would like to know how to make the switch statement to ask me if i want to either add more phone numbers or like a loop for the switch cases. also, how can i make the program to search for a name inside the binary file. i tried my best but i just cant make it work.

问题回答

Thou 核实甲状腺文档运行成功! Hint:如果你真的想要战斗机会,使书面行动取得成功,那么你可能想考虑打开档案书写方式。 既然如此,我会感到惊讶的是,你会设法向投入文件撰写文章。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签