Hi!
For my last assignment I was told do create a program which can read-, sort- sand save a textfile (.txt).
For example, I have a textfile containing 5 movies, this file is going to be read by the program and then sorted (in alphabetical order) and then saved to a new file.
My textfile (movies.txt) is the following:
Jurassic Park
Superman Returns
Batman Begins
The Simpsons
The Shawshank Redemption
Once sorted it must look like this (sorted_movies.txt):
Batman Begins
Jurassic Park
Superman Returns
The Shawshank Redemption
The Simpsons
Here is what I came up with:
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
void sort(char *movies[])
{
int pass, i;
char *temp;
const int size = 5;
for (pass=0; pass < size - 1; pass++)
{
for (i=0; i<size - 1; i++) //
{
if (strcmp(movies[i], movies[i+1]) > 0)
{
temp = movies[i];
movies[i] = movies[i+1];
movies[i+1] = temp;
}
}
}
}
//---------------------------------------------------------------------------------------------
int main()
{
ifstream ifil ("movies.txt");
int Quantity = 0;
string Word;
while ( ifil >> Word ) {
Quantity++;
}
cout << "Read " << Quantity << " words." << endl;
int i;
char *list[] = {"e", "c", "d", "b", "a"};
sort(list);
ifil.close();
ofstream ofile;
ofile.open("sorted_movies.txt");
cout<<"Sorted in order \n"<<endl;
ofile<<"Sorted in order\n"<<endl;
for (i = 0; i < sizeof(*list)+1; i++)
{
cout<<i<<" "<<list[i]<<endl;
ofile<<i<<" "<<list[i]<<endl;
}
ofile.close();
return 0;
}
I cant get it to work, and I must read lines not words :/