sort a txt file and insert into another txt file

hi i have a project and mainly what i need to do is i have a (input)txt file with a bunch of random numbers unsorted and then i will store those numbers into an array, sort them and then put the sorted numbers into a different (output)txt file. i kinda understand for the most part but i know im missing stuff i know i need to actually insert the numbers into the (output)txt file and im kinda just stuck on how to go about it. any input would be such a big help and much appriciated. this is my code so far...

#include <iostream>
#include <fstream>
using namespace std;
void ReadArray(ifstream &inputFile, int array[], int &size);
void SortArray(int array[], int size);
void swap(int &x, int &y);
void WriteArray(ofstream &outputfile, int array[], int size);

int main()
{

const int MAXSIZE = 1000;
int size=0;
int array[MAXSIZE];
int sorted[MAXSIZE];

//decaring the input file stream
ifstream inputFile;
//declaring the output file stream
ofstream outputFile;


inputFile.open("numbers.txt");
outputFile.open("sorted.txt");

ReadArray(inputFile, array, size);

inputFile.close();

SortArray(array, size);
WriteArray(outputFile, array, size);

outputFile.close();

return 0;
}

void ReadArray(ifstream &inputFile, int array[], int &size)
{

size = 0;
while (!inputFile >> array[size])
{
size++;
}

}


void swap(int &x, int &y)
{
int temp;
temp = x;
x = y;
y =temp;
}

void SortArray(int array[], int size)

{
int i, j;
for(i=0; i<size;i++)
{
for(j=0; j<size-1; j++)
{
if(array[j] > array[j+1])
{
swap(array[j], array[j+1]);
}
}
}
}

void WriteArray(ofstream &outputFile, int array[], int size)
{
int index;
for (index = 0; index < size; index++)
{
outputFile << array[index] << endl;
}
}

[code] "Your code goes here" [/code]
1
2
3
4
5
6
7
8
9
void ReadArray(ifstream &inputFile, int array[], int &size)
{
	size = 0;
//	while (!inputFile >> array[size])
	while( inputFile>>array[size] )
	{
		size++;
	}
}

You may want to check
http://www.cplusplus.com/reference/algorithm/swap/
http://www.cplusplus.com/reference/algorithm/sort/

http://www.cplusplus.com/reference/stl/vector/
Last edited on
Topic archived. No new replies allowed.