/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <vector>
#include <fstream>

#define INPUT_FILE "input.txt"
#define OUTPUT_FILE "output.txt"

#if 1
int main(void)
{

    /*
    Le programme ouvre le fichier input.txt,
    charge tout les "mots" qu'il contient dans une liste
    et les reecrit ligne par ligne en orde inverse dans output.txt.
    */

	std::vector<char*> list;
    FILE *input, *output;
    char *word;

    input = fopen(INPUT_FILE, "r");

    while (1)
    {
        word = (char *)malloc(5+1);
        if ((fscanf(input, "%s", word)) == EOF)
        {
            free(word);
            break;
        }
        /* insertion toujours au debut pour pouvoir parcourir
        dans un ordre logique (debut -> fin) lors de la reecriture */
        list.push_back(word);
    }

    fclose(input);

    output = fopen(OUTPUT_FILE, "wb");

    for(std::vector<char*>::reverse_iterator it = list.rbegin(); it != list.rend(); ++it)
    {
        fprintf(output, "%s\n", *it);

        delete [] *it;
    }

    fclose(output);

    return 0;
}

#else
int main()
{
	std::ifstream input(INPUT_FILE);
	std::vector<char*> list;

	std::string line;
	while(std::getline(input, line))
	{
		char* word = new char[5+1];
		strcpy(word, line.c_str());
		list.push_back(word);
	}

	std::ofstream output(OUTPUT_FILE);
	for(std::vector<char*>::reverse_iterator it = list.rbegin(); it != list.rend(); ++it)
	{
		output << *it << std::endl;
		delete [] *it;
	}
	return 0;
}
#endif
