C++ FSTREAM & classes
<p>Okay so when I started using classes to make functions, it gives me an error file is invalid, normally doesn't if I don't use classes. If you run it, enter in some info, and then run it again, normally it should read in the data correctly, it still reads it in, but it says there's an error. I don't really know what I'm doing wrong, any tips? Thanks!</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;cstring&gt;
#include &lt;cctype&gt;
using namespace std;

const int TEMP_SIZE = 10;
const int NAME_SIZE = 100;
const int BREED_SIZE = 100;
const int DESC_SIZE = 250;
const int REASON_SIZE = 250;
const int ID_SIZE = 50;

//creating the struct
struct animal
{
    char name[NAME_SIZE];
    char ID[ID_SIZE];
    char breed[BREED_SIZE];
    float age;
    float weight;
    char desc[DESC_SIZE];
    char reason[REASON_SIZE];
    int day;
    int month;
    int year;
    float length;
};

struct adopted
{
    char hostName[100];
    char hostAddress[100];
    int numPets;
};

class petAdoption
{
    public:
        petAdoption();
        //~petAdoption();
        void enroll(animal newAnimal[]);
        void read(animal newAnimal[]);
    private:
        int count;
        int numPets;
        int * pets;
};

petAdoption::petAdoption()
{
    count = 0;
    pets = NULL;
    numPets = 0;
}

void petAdoption::enroll(animal newAnimal[])
{
            cout &lt;&lt; "Please enter your pet's name: ";
            cin.get(newAnimal[count].name, NAME_SIZE, '\n');
            cin.ignore(100, '\n');

            cout &lt;&lt; "Please enter a unique ID for your pet (combinations of numbers EG: 432FD3): ";
            cin.get(newAnimal[count].ID, ID_SIZE, '\n');
            cin.ignore(100, '\n');

            cout &lt;&lt; "What type of breed is your pet?: ";
            cin.get(newAnimal[count].breed, BREED_SIZE, '\n');
            cin.ignore(100, '\n');

            cout &lt;&lt; "How old is your pet?: ";
            cin &gt;&gt; newAnimal[count].age;
            cin.ignore(100, '\n');

            cout &lt;&lt; "How much does your pet weigh? (in LBS): ";
            cin &gt;&gt; newAnimal[count].weight;
            cin.ignore(100, '\n');

            cout &lt;&lt; "Please describe your pet's personality!: ";
            cin.get(newAnimal[count].desc, DESC_SIZE, '\n');
            cin.ignore(100, '\n');

            cout &lt;&lt; "Please explain why the pet is being put up for adoption: ";
            cin.get(newAnimal[count].reason, REASON_SIZE, '\n');
            cin.ignore(100, '\n');

            cout &lt;&lt; "Please enter your pet's day of birth (1-31): ";
            cin &gt;&gt; newAnimal[count].day;
            cin.ignore(100, '\n');

            cout &lt;&lt; "Please enter your pet's month of birth (1-12): ";
            cin &gt;&gt; newAnimal[count].month;
            cin.ignore(100, '\n');

            cout &lt;&lt; "Please enter your pet's year of birth (1900-2012) : ";
            cin &gt;&gt; newAnimal[count].year;
            cin.ignore(100, '\n');

            cout &lt;&lt; "Please enter the length of time your pet has spent in a shelter (in months): ";
            cin &gt;&gt; newAnimal[count].length;
            cin.ignore(100, '\n');
/*** WRITES the pet ID into the list of pets***/
            ofstream write;
            write.open("pets.txt", ios::app);
            write &lt;&lt; newAnimal[count].name &lt;&lt; '\n';
            write.close();

/*** WRITES EACH PET INFO ****/
//this opens the file / creates a file if it's not made yet
//and it writes the pet's information
        write.open(newAnimal[count].name, ios::app);    
        write &lt;&lt; newAnimal[count].name &lt;&lt; '\n'
        &lt;&lt; newAnimal[count].ID &lt;&lt; '\n'
        &lt;&lt; newAnimal[count].breed &lt;&lt; '\n'
        &lt;&lt; newAnimal[count].age &lt;&lt; '\n'
        &lt;&lt; newAnimal[count].weight &lt;&lt; '\n'
        &lt;&lt; newAnimal[count].desc &lt;&lt; '\n'
        &lt;&lt; newAnimal[count].reason &lt;&lt; '\n'
        &lt;&lt; newAnimal[count].day &lt;&lt; '\n' 
        &lt;&lt; newAnimal[count].month &lt;&lt; '\n'   
        &lt;&lt; newAnimal[count].year &lt;&lt; '\n'    
        &lt;&lt; newAnimal[count].length &lt;&lt; '\n';
//this closes the file
        write.close();
}

void petAdoption::read(animal newAnimal[])
{
    ifstream read;
//open the file apps.txt
    read.open("pets.txt");
//if apps.txt doesn't exist, then print out this error
    if(!read)
    {
        cout &lt;&lt; "pets.txt doesn't exist! This is your first time!" &lt;&lt;endl;
    }
//else if it does exist, read in the names and store them back
//into the struct member name(s)
    else 
    {
//while the document isn't empty
//read in each line
        while(!read.eof())
        {
        read.getline(newAnimal[count].name, NAME_SIZE, '\n');
        ++count;
        }
        count = count-1;
    }
//close the file
    read.close();
    for (int i = 0; i &lt; count; ++i)
    {
//open the file of the name of the app
        read.open(newAnimal[count].name);
//if the file doesn't exist
//then we probably deleted it
//without removing it from the apps.txt
//but it prints out an error
        if( !read)
        {
            cout &lt;&lt; "invalid file!" &lt;&lt;endl;
        }
//however if the file does exist, 
//read in each line and store them back
//into the struct members
        while(!read.eof())
        {
            read.getline(newAnimal[count].name, NAME_SIZE, '\n');
            read.ignore(100, '\n');
            read.getline(newAnimal[count].ID, ID_SIZE, '\n');
            read.ignore(100, '\n');
            read.getline(newAnimal[count].breed, BREED_SIZE, '\n');
            read.ignore(100, '\n');
            read &gt;&gt; newAnimal[count].age;
            read.ignore(100, '\n');
            read &gt;&gt; newAnimal[count].weight;
            read.ignore(100, '\n');
            read.getline(newAnimal[count].desc, DESC_SIZE, '\n');
            read.ignore(100, '\n');
            read.getline(newAnimal[count].reason, REASON_SIZE, '\n');
            read.ignore(100, '\n');
            read &gt;&gt; newAnimal[count].day;
            read &gt;&gt; newAnimal[count].month;
            read &gt;&gt; newAnimal[count].year;
            read &gt;&gt; newAnimal[count].length;
            read.ignore(100, '\n');
        }
//close the file
        read.close();
    }
}

int main()
{
    animal newAnimal[10];
    petAdoption adopt;
    adopt.read(newAnimal);
    adopt.enroll(newAnimal);    
}
</code></pre>
