Need help with a template function to output an array to a data file- urgent!

I need help with the outputData function.  The code is compiling, but when I check the file "integersOut.txt", it is empty.  Here is my code:

#ifndef H_listType
#define H_listType

#include <iostream>
using namespace std;

template <class elemType>
class listType
{
public:
    bool getData(char* filename);
        //loads array from file
   bool outputData(char* filename);
        //writes list to output file
    bool isEmpty();
        //Returns true if the list is empty, false otherwise
    bool isFull();
        //Returns true if the list is full, false otherwise
    int getLength();
        //Returns the length of the list
    int getMaxSize();
        //Returns the maximum number of elements that can be stored in the list
    void sort();  
          //Sorts the list
   void print() const;
          //Outputs the elements of the list
    void insert(const elemType& insertItem);
        //inserts item into list
   void removeAt(int location);
        //removes item at specified location from list
   void remove(const elemType& removeItem);
            // removes item from list
   int seqSearch(const elemType& item);
            //searches list for a specified item
    listType(int listSize = 50);
         //constructor
  ~listType();
          //destructor
          
private:
    int maxSize; //maximum number that can be stored in the list
    int length;  //number of elements in the list
    elemType *list; //pointer to the array that holds the list elements
};

template<class elemType>
bool listType<elemType>::outputData(char* filename)
{
      ofstream outfile(filename, ios::out);
      if(outfile.fail())
      {
            cerr<<"Error opening output file!\n";
            return false;
      }

      outfile.open(filename, ios::out);
      for(int i = 0; i < length; i++)
      outfile << list[i] << " " << endl;
                outfile.close();
      return true;
}

template<class elemType>
bool listType<elemType>::getData(char* filename)
{
    elemType temp;
    int i = 0;

    ifstream infile(filename, ios::in);
    if(infile.fail())
    {
         cerr<<"Error opening input file!\n";
         return false;
    }

    while(!infile.eof() && i < maxSize)
    {
         infile>>temp;
         insert(temp);

         ++i;
    }
    infile.close();
    return true;
}


template<class elemType>
bool listType<elemType>::isEmpty()
{
      return (length == 0);
}

template<class elemType>
bool listType<elemType>::isFull()
{
      return (length == maxSize);
}

template<class elemType>
int listType<elemType>::getLength()
{
      return length;
}

template<class elemType>
int listType<elemType>::getMaxSize()
{
      return maxSize;
}

template<class elemType>
listType<elemType>::listType(int listSize)
{
      maxSize = listSize;
      length = 0;
      list = new elemType[maxSize];
}

template<class elemType>
listType<elemType>::~listType()  //destructor
{
      delete [] list;
}

template<class elemType>
void listType<elemType>::sort()   //selection sort
{
      int i, j;
      int min;
      elemType  temp;

      for(i = 0; i <length; i++)
      {
            min = i;
            for(j = i+1; j < length; ++j)
               if(list[j] < list[min])
                  min = j;
            temp = list[i];
            list[i] = list[min];
            list[min] = temp;
      }
}

template<class elemType>
void listType<elemType>::print() const
{
      int i;
      for(i = 0; i < length; ++i)
            cout<<list[i]<<"  ";
      cout<<endl;
}

template<class elemType>
void listType<elemType>::insert(const elemType& insertItem)
{
      if (length >= maxSize)
            cout<<"Cannot insert in a full list."<<endl;
      else
            list[length] = insertItem;
            length++;
}

template<class elemType>
void listType<elemType>::removeAt(int location)
{
      int i;

      if(location < 0 || location >= length)
          cout<<"The location of the item to be removed "
                  <<"is out of range"<<endl;
      else
      {
               for(i = location; i < length - 1; i++)
                   list[i] = list[i+1];

            length--;
      }
}

template<class elemType>
void listType<elemType>::remove(const elemType& removeItem)
{
      int loc;
      if(length == 0)
            cout <<"Cannot delete from an empty list."<<endl;
      else
      {
            loc = seqSearch(removeItem);

            if(loc != -1)
                  removeAt(loc);
            else
                  cout<<"The item to be deleted is not in the list."<<endl;
      }
}

template<class elemType>
int listType<elemType>::seqSearch(const elemType& item)
{
      int loc;
      bool found = false;

      for(loc = 0; loc < length; loc++)
            if(list[loc] == item)
            {
                  found = true;
                  break;
            }
      if(found)
            return loc;
      else
            return -1;
}


#endif
___________________________________

#include <iostream>
#include <fstream>
#include "listType.h"
#include "myString.h"
using namespace std;

int main()
{
      int number;      
      listType<int> intList(100);      
      intList.getData("G:\\integers.txt");
                                                                        
      cout<<"The list of integers found in file integers.txt is: "<<endl;      
      intList.print();                                                      
      cout<<endl;                                                                  

      cout<<"After sorting, the list is: "<<endl;                                                                  
      intList.sort();                                                      
      intList.print();                                                      
      cout<<endl;
      cout<<endl;
      cout<<"Length of the list = " <<intList.getLength()<<endl;                              
      cout<<"Maximum size of the list = "      <<intList.getMaxSize()<<endl<<endl;

      cout<<"Enter the value you would like to search for:"<<endl;
      cin>>number;
      if (intList.seqSearch(number) == -1)
            cout<<"The value you entered is not in the list.\n\n";
      else
               cout<< "The value you entered is at location " << intList.seqSearch(number) << ".\n\n";

      cout<< "Enter the item you would like to add to the list:"<<endl;
      cin>>number;
      intList.insert(number);
      cout<<"The new list is: "<<endl;
      intList.print();
      cout<<"After sorting, the new list is: "<<endl;                                                                  
      intList.sort();                                                      
      intList.print();
      cout<<endl;

      cout<< "Enter the item you would like to remove from the list:"<<endl;
      cin>>number;
      intList.remove(number);
      cout<<"The new list is: "<<endl;
      intList.print();
      cout<<endl;
      cout<<endl;
      intList.outputData("G:\\integersOut.txt");
      
      return 0;
}
coririzzoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteHCommented:
You open file twice: one inside the constructor and once explicitly afterwards. The second one should fail which you don't check.
So remove the second opening statement:
bool listType<elemType>::outputData(char* filename)
{
     ofstream outfile(filename, ios::out);
     if(outfile.fail())
     {
          cerr<<"Error opening output file!\n";
          return false;
     }

     // outfile.open(filename, ios::out); // it is already open if you got here.
     for(int i = 0; i < length; i++)
     outfile << list[i] << " " << endl;
                outfile.close();
     return true;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
coririzzoAuthor Commented:
I have really been working on this too long!  Big DUH!  Thanks!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.