•
•
•
•
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
![]() |
•
•
Join Date: Jun 2005
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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???
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).
•
•
Join Date: Jun 2005
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Heres my code..
http://users.igl.net/poollegends/hmm/qwerty.txt
sortData function....
email me please.. kuenre@hotmail.com thanks
http://users.igl.net/poollegends/hmm/qwerty.txt
sortData function....
email me please.. kuenre@hotmail.com thanks
First, get rid of the extraneous semicolon.
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.
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.
}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.
•
•
Join Date: Jun 2005
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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..
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?
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 to this And this to this
sortArray( recordArray, MAX );sortArray( recordArray, i );for ( int i = 0; i < 5; i++ )for ( int i = 0; i < m; i++ )![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Completed Code but A LOT of GOTO...Replacement?
- Next Thread: Email crawler Software



Linear Mode