Hi. Im doing a c++ course. And we are doing a assignment to do with I/O File streams. We are using struct. I can add, update, print, and search for records. Now, I need help to read data into an array and sort the array on any one field and the read the data to a new file. Data consist of:

ID Name Age Height


Any advice/help???

Post a sample of the data file and a minimal code that attempts to read the data (for example: the structure and array definitions, your read routine, and a call of the read routine from main).

First, get rid of the extraneous semicolon.

void sortData( );
{
   person temp;
   char dataArray[ 50 ];

   MyFile.seekg( 0, ios::beg );

   MyFile.read( ( char* ) &temp, sizeof( person ) );

   //here i need to put in a array... and sort.. then put back in a new file.
}

You'll do file reading much like you do elsewhere in the code.

Why the global fstream?
Why a binary file?
Do you know about the issue with loop control and .eof()?

>email me please
No. Read the rules.

Global fstream is part of the specification from my tutor...same with binary file. issue with eof loop?????

I got it too read to an array of structures..

//---------------------------------------------------------------------------
void readData()
{
   const int MAX = 30;
   person recordArray[ MAX ];
   person temp;
   int i;

   clrscr();

   MyFile.seekg( 0, ios::beg );

   MyFile.read( ( char* ) &recordArray[ 0 ], sizeof( person ) );

   i = 0;
   while( !MyFile.eof() )
   {
      cout << recordArray[ i ].id << "  ";
      cout << recordArray[ i ].firstName << "  ";
      cout << recordArray[ i ].lastName << "  ";
      cout << recordArray[ i ].age << "  ";
      cout << recordArray[ i ].height << "  ";
      cout << recordArray[ i ].dateTime << endl;

      i++;
      MyFile.read( ( char* ) &recordArray[ i ], sizeof( person ) );
   }

   sortArray( recordArray, MAX );
   MyFile.clear();
   getch();
}
//---------------------------------------------------------------------------
void sortArray( person recordArray [], int m )
{
   int position;
   const int MAX = 3;
   person tempArray[ MAX ];

   for ( int i = 0; i < 5; i++ )
   {
      cout << recordArray[ i ].id << "  ";
      cout << recordArray[ i ].firstName << "  ";
      cout << recordArray[ i ].lastName << "  ";
      cout << recordArray[ i ].age << "  ";
      cout << recordArray[ i ].height << "  ";
      cout << recordArray[ i ].dateTime << endl;
   }

   for ( int i = 0; i < m - 1; i++ )
   {
      position = i;
      for ( int j = i; j < m; j++ )
      {
         if ( recordArray[ j ].age < recordArray[ position ].age )
            position = j;
      }
      if ( recordArray[ position ].age < recordArray[ i ].age )
      {
         tempArray[ 0 ] = recordArray[ i ];
         recordArray[ i ] = recordArray[ position ];
         recordArray[ position ] = tempArray[ 0 ];
      }
   }

   for ( int i = 0; i < 5; i++ )
   {
      cout << recordArray[ i ].id << "  ";
      cout << recordArray[ i ].firstName << "  ";
      cout << recordArray[ i ].lastName << "  ";
      cout << recordArray[ i ].age << "  ";
      cout << recordArray[ i ].height << "  ";
      cout << recordArray[ i ].dateTime << endl;
   }

   getch();
}
//---------------------------------------------------------------------------

In sortArray, its doing an Exhange sort... When it swaps and I print recordArray, it prints garbage.. I want to sort this on the height field.. this is a float, that returned an error, so now im doing it on the age field... can you help?

Do you have MAX items? You might want to change this

sortArray( recordArray, MAX );

to this

sortArray( recordArray, i );

And this

for ( int i = 0; i < 5; i++ )

to this

for ( int i = 0; i < m; i++ )

Hi, Thank you so much for your help...

The problem was that the 'project1.exe' file was old and wasnt updating, so i deleted it and open the cpp in a new project.. and now it works fine!!

Thanks and sorry if I wasted any of your time...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.