Hey guys im having a bit of trouble with bubble sorting an array from a file...
Basically what needs to be done is i need to read a text file into a struct array and then copy it into a new file after sorting it by a string field

can you help me please this is what i have so far

//--------------------------------------------------------------------------
// void
// Purpose:
// Inputs:
// Ouputs:
//---------------------------------------------------------------------------
void sort(int item[], int size)
{
	clrscr();

	int swaps;
	int temp;
	int left;
	int right;

	do
	{
		swaps = 0;
		left = 0;
		right = 1;

		while (right < size)
		{
			if (item[ right ] < item [left])
			{
				temp = item[ left ];
				item[ left ] = item[ right ];
				item[ right ] = temp;
				swaps++;
			}
			left++;
			right++;
      }

	} while (swaps > 0);







	getch();

}
//---------------------------------------------------------------------------
// void
// Purpose:
// Inputs:
// Ouputs:
//--------------------------------------------------------------------------
char archive()
{
	clrscr();

	fstream recordFile;
	recordFile.open("H:\\records.txt", ios::binary | ios::out | ios::in);
	fstream archiveFile;

	participant person;
	char confirm;

	cout << "**********ARCHIVE A RECORD**********\n\n" ;

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

	cout << "Are you sure you want to archive the file?";
	cin >> confirm;

	if (tolower(confirm) == 'y' || tolower(confirm) == 'yes')
	{

		archiveFile.open("H:\\archive.txt", ios::binary | ios::out );
		archiveFile.close();
		cout << "\nPlease wait while we archive the record...\n\n";
		archiveFile.open("H:\\archive.txt", ios::binary | ios::in | ios::out);

		recordFile.read((char*) &person, sizeof (participant));

		while(!recordFile.eof())
		{
			archiveFile.write((char*) &person, sizeof(participant));
			recordFile.read((char*) &person, sizeof (participant));
		}
		archiveFile.close();
		cout << "\n---The file has been archived---";
		return (person);
	}
	else
	{
		cout << "\n---You have chosen NOT to archive this folder---";
   }

	recordFile.close();

	getch();


}

Hey guys im having a bit of trouble with bubble sorting an array from a file...

Not very helpful that line is my dear friend. Mention you should what the exact problem you are facing, what is the output you are getting.
And honestly, say i must, most complicated implementation of bubble sort this is.. thought of using for loops have u?

Comments
Thanks Master Yoda

lol bro... ok heres the thing i need to archive the file which i can do what i dont know is how to read the file into a struct array ie (participant) person; which needs to turn into an array... and then pass it to sort have it bubble sort the current file then pass it to archive and archive that file.

as for the bubble sort im a student and this is the format we were told to use

Can you post the structure and the input file extract. From what I understand, you will have to read the file one logical line at a time, parse the line, populate the structure from the data and insert into the array. Then in sort, iterate over the array and compare pairs of structures based on the field on which you have to sort. Then write the sorted array onto the output file.

yea thats what i gotta do but i go NO clue how to... do you want the whole code??

Why don't you try one step at a time and post the code if you face any problem. Start with reading a file line by line and printing it out. Then try and see how you can extract values out of it to populate a structure and so on. I can't solve it for you so you will have to learn to do it and we can help you with issues when you don't find answers in the book.

i dont know how to read it into an array thats what i need help with... the books they gave us dont cover that... i dont need the whole code i just need the array bit once thats done im home free

my one friend sugested doing it this way (my stuct btw is participant and variable is person) participant* archive (archive being the function) then passing it thru some how to sort but not really knowing how...

or do you think something like this would be better

have a while loop that reads the records into a struct one at a time then passes that to the sort

while (!recordFile.close())
{
   recordFile.read((char*) &person, sizeof (participant));
   sort(person); // paticipant is a gloabal
   recordFile.read((char*) &person, sizeof (participant));
   while(!recordFile.eof())
   {
   archiveFile.write((char*) &person, sizeof(participant));
   recordFile.read((char*) &person, sizeof (participant));
   }
   archiveFile.close();
   cout << "\n---The file has been archived---";
   recordFile.close;
   return (person);
}

does that look like it would work?

if i modify the sort to fit in that situation

This article has been dead for over six months. Start a new discussion instead.