C++ 将一个结构值复制到另一个

标签 c++ copy structure

我有一个简单的结构:

struct Appartament
{
  char address[50];
  char telephoneNumber[20];
  char view[10];

  double price;
  double distanceFromCenter;

  int roomCount;
};

我有一些记录写在一个文件里。现在,我想从文件中读取所有记录,并只获取 roomCount 小于数字(用户输入)的记录。这很简单,但记录应该按价格排序显示。这就是我必须将它们放在一个数组中然后对它们进行排序的方式。

我有一些问题,我相信它们是因为我没有很好地处理结构。

我尝试了不同的方式:

strcpy(CurrentRecords[index].address,currentRecord.address);
strcpy(CurrentRecords[index].telephoneNumber,currentRecord.telephoneNumber);
strcpy(CurrentRecords[index].view,currentRecord.view);
CurrentRecords[index].price=currentRecord.price;
CurrentRecords[index].distanceFromCenter=currentRecord.distanceFromCenter;
CurrentRecords[index].roomCount=currentRecord.roomCount;

memcpy(CurrentRecords[index],currentRecord,sizeof(Appartament));

CurrentRecords[index]=currentRecord

但没有任何作用...

编辑:这是我的代码 - “没有任何效果”指的是无限循环之类的东西。

    void AdvanceSearch()
    {
        clrscr();

        Appartament currentRecord;

        fstream currentFile("Records.dat",ios::binary|ios::in);

        if(!currentFile)
        {
            cout<<"Error - the file could not be opened."<<endl;
            return;
        }
        else
        {
            //Array with apartments records
            Appartament CurrentRecords[MaxRecords];

            currentFile.seekg(0L,ios::end);
            long int length=currentFile.tellg();
            currentFile.seekg(0L,ios::beg);

            int isAppartamentFound=0;

            if(length==0)
            {
                cout<<"The file is empty."<<endl;
                return;
            }
            else
            {
                int userRoomCount;

                do
                {
                    clrscr();
                    cout<<"Enter apartment room count - ";
                    cin>>userRoomCount;
                }while(userRoomCount<0);

                clrscr();

                cout<<endl<<"Apartments with "<<userRoomCount<<" rooms order by price:";

                currentFile.read((char*)(&currentRecord),sizeof(Appartament));

                int index=0;

                while(!currentFile.eof())
                {
                    if(currentRecord.roomCount==userRoomCount)
                    {

                         /*
                        strcpy(CurrentRecords[index].address,currentRecord.address);
                        strcpy(CurrentRecords[index].telephoneNumber,currentRecord.telephoneNumber);
                        strcpy(CurrentRecords[index].view,currentRecord.view);
                        CurrentRecords[index].price=currentRecord.price;
                        CurrentRecords[index].distanceFromCenter=currentRecord.distanceFromCenter;
                        CurrentRecords[index].roomCount=currentRecord.roomCount;
                         */

                         memcpy(CurrentRecords[index],currentRecord,sizeof(Appartament));

                        //CurrentRecords[index]=currentRecord;
                        index++;
                        isAppartamentFound=1;
                    }

                    currentFile.read((char*)(&currentRecord),sizeof(Appartament));
                }

                currentFile.close();
            }

            if(isAppartamentFound==0)
            {
                cout<<endl<<"There are no matches!"<<endl;
            }
            else
            {
                //If only one apartment is found
                if(sizeof(CurrentRecords)/sizeof(Appartament)==1)
                {
                    cout<<endl;
                    ShowRecord(currentRecord);

                }
                else
                {
                    //Sort the records

                    Appartament tempApartament;

                    int isChangeMade=1;

                    while(isChangeMade==1)
                    {
                        isChangeMade=0;

                        for(int index=0;index<(sizeof(CurrentRecords)/sizeof(Appartament))-1.0;index++)
                        {
                            if(CurrentRecords[index].price>CurrentRecords[index+1].price)
                            {
                                isChangeMade=1;

                                CopyApartament(tempApartament,CurrentRecords[index]);
                                CopyApartament(CurrentRecords[index],CurrentRecords[index+1]);
                                CopyApartament(CurrentRecords[index+1],tempApartament);
                            }
                        }
                    }

                    for(int index=0;index<sizeof(CurrentRecords)/sizeof(Appartament)-1.0;index++)
                    {
                        ShowRecord(CurrentRecords[index]);
                    }
                }
            }
         }
    }

    void CopyApartament(Appartament RecordOne,Appartament RecordTwo)
    { 
                    /*
        strcpy(RecordOne.address,RecordTwo.address);
        RecordOne.distanceFromCenter=RecordTwo.distanceFromCenter;
        RecordOne.price=RecordTwo.price;
        RecordOne.roomCount=RecordTwo.roomCount;
        strcpy(RecordOne.telephoneNumber,RecordTwo.telephoneNumber);
        strcpy(RecordOne.view,RecordTwo.view);
                    */
                    RecordOne=RecordTwo;

    }

注意:我认为我的问题是复制,因为我不知道该怎么做。

最佳答案

CurrentRecords[index]=currentRecord;

复制了整个结构并且应该按预期工作。

你确定你排序正确吗?

关于C++ 将一个结构值复制到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10624147/

相关文章:

c++ - 从另一个线程渲染到 QWindow

c++ - 如何解决退出状态1错误?

Java 如何将大数组复制到小数组?

c++ - 将结构的动态数组传递给 GPU 内核

c# - 无法设置 MQL_DLLS_ALLOWED

c++ - 通过 C++ 中的工厂传递模拟

c++ - 类复制操作,它是如何工作的?

c# - 使用远程管理员凭据将文件复制到远程计算机

c - C 中接受带有十进制值的较大数字

c - 为什么 inet_ntoa 在这段代码中返回 0.0.0.0 ?