User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 429,785 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,917 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2937 | Replies: 6
Reply
Join Date: Jun 2005
Posts: 4
Reputation: myprecious is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
myprecious myprecious is offline Offline
Newbie Poster

Input Output File Streams - Sorting Help With Array

  #1  
Jun 7th, 2005
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???
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,657
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 145
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Input Output File Streams - Sorting Help With Array

  #2  
Jun 7th, 2005
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).
Reply With Quote  
Join Date: Jun 2005
Posts: 4
Reputation: myprecious is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
myprecious myprecious is offline Offline
Newbie Poster

Re: Input Output File Streams - Sorting Help With Array

  #3  
Jun 8th, 2005
Heres my code..

http://users.igl.net/poollegends/hmm/qwerty.txt

sortData function....
email me please.. kuenre@hotmail.com thanks
Reply With Quote  
Join Date: Apr 2004
Posts: 3,657
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 145
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Input Output File Streams - Sorting Help With Array

  #4  
Jun 8th, 2005
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.
Reply With Quote  
Join Date: Jun 2005
Posts: 4
Reputation: myprecious is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
myprecious myprecious is offline Offline
Newbie Poster

Re: Input Output File Streams - Sorting Help With Array

  #5  
Jun 10th, 2005
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?
Reply With Quote  
Join Date: Apr 2004
Posts: 3,657
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 145
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Input Output File Streams - Sorting Help With Array

  #6  
Jun 10th, 2005
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++ )
Reply With Quote  
Join Date: Jun 2005
Posts: 4
Reputation: myprecious is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
myprecious myprecious is offline Offline
Newbie Poster

Re: Input Output File Streams - Sorting Help With Array

  #7  
Jun 10th, 2005
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...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:08 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC