title: C++ Copy one struct value to other
body: <p>I have a simple structure:</p>
  
  <pre><code>struct Appartament
  {
    char address[50];
    char telephoneNumber[20];
    char view[10];
  
    double price;
    double distanceFromCenter;
  
    int roomCount;
  };
  </code></pre>
  
  <p>I have some records written in a file. Now, I want to read all records from the file and to get only this who have roomCount smaller then a number (user input). This is easy, but the records should be displayed sorted by price. That's way I have to put them in a array and after that to sort them. </p>
  
  <p>I have some problems and I believe that they are because I am not copping the structures well.</p>
  
  <p>I have try different ways:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>or </p>
  
  <pre><code>memcpy(CurrentRecords[index],currentRecord,sizeof(Appartament));
  </code></pre>
  
  <p>and </p>
  
  <pre><code>CurrentRecords[index]=currentRecord
  </code></pre>
  
  <p>but nothing works...</p>
  
  <p>EDIT: Here is the my code - "nothing works" refers to something like endless loop.</p>
  
  <pre><code>    void AdvanceSearch()
      {
          clrscr();
  
          Appartament currentRecord;
  
          fstream currentFile("Records.dat",ios::binary|ios::in);
  
          if(!currentFile)
          {
              cout&lt;&lt;"Error - the file could not be opened."&lt;&lt;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&lt;&lt;"The file is empty."&lt;&lt;endl;
                  return;
              }
              else
              {
                  int userRoomCount;
  
                  do
                  {
                      clrscr();
                      cout&lt;&lt;"Enter apartment room count - ";
                      cin&gt;&gt;userRoomCount;
                  }while(userRoomCount&lt;0);
  
                  clrscr();
  
                  cout&lt;&lt;endl&lt;&lt;"Apartments with "&lt;&lt;userRoomCount&lt;&lt;" rooms order by price:";
  
                  currentFile.read((char*)(&amp;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*)(&amp;currentRecord),sizeof(Appartament));
                  }
  
                  currentFile.close();
              }
  
              if(isAppartamentFound==0)
              {
                  cout&lt;&lt;endl&lt;&lt;"There are no matches!"&lt;&lt;endl;
              }
              else
              {
                  //If only one apartment is found
                  if(sizeof(CurrentRecords)/sizeof(Appartament)==1)
                  {
                      cout&lt;&lt;endl;
                      ShowRecord(currentRecord);
  
                  }
                  else
                  {
                      //Sort the records
  
                      Appartament tempApartament;
  
                      int isChangeMade=1;
  
                      while(isChangeMade==1)
                      {
                          isChangeMade=0;
  
                          for(int index=0;index&lt;(sizeof(CurrentRecords)/sizeof(Appartament))-1.0;index++)
                          {
                              if(CurrentRecords[index].price&gt;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&lt;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;
  
      }
  </code></pre>
  
  <p>Note: I thought that my problem is the copying because I have not know how to do this.</p>