0

好的,所以当我开始使用类来制作函数时,它给了我一个错误文件无效,如果我不使用类通常不会。如果你运行它,输入一些信息,然后再次运行它,通常它应该正确读取数据,它仍然读取它,但它说有一个错误。我真的不知道我做错了什么,有什么提示吗?谢谢!

#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
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 << "Please enter your pet's name: ";
            cin.get(newAnimal[count].name, NAME_SIZE, '\n');
            cin.ignore(100, '\n');

            cout << "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 << "What type of breed is your pet?: ";
            cin.get(newAnimal[count].breed, BREED_SIZE, '\n');
            cin.ignore(100, '\n');

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

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

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

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

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

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

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

            cout << "Please enter the length of time your pet has spent in a shelter (in months): ";
            cin >> 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 << newAnimal[count].name << '\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 << newAnimal[count].name << '\n'
        << newAnimal[count].ID << '\n'
        << newAnimal[count].breed << '\n'
        << newAnimal[count].age << '\n'
        << newAnimal[count].weight << '\n'
        << newAnimal[count].desc << '\n'
        << newAnimal[count].reason << '\n'
        << newAnimal[count].day << '\n' 
        << newAnimal[count].month << '\n'   
        << newAnimal[count].year << '\n'    
        << newAnimal[count].length << '\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 << "pets.txt doesn't exist! This is your first time!" <<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 < 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 << "invalid file!" <<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 >> newAnimal[count].age;
            read.ignore(100, '\n');
            read >> 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 >> newAnimal[count].day;
            read >> newAnimal[count].month;
            read >> newAnimal[count].year;
            read >> 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);    
}
4

1 回答 1

0

我想我知道发生了什么。当你说 read.open("pets.txt"); 它应该说 read.open("pets.txt"),ios::app 因为那样它就不会删除它已经存储的数据。还可以进入文件资源管理器并搜索 pets.txt 并阅读它以查看发生了什么。我希望我对你有所帮助。

于 2013-11-03T10:52:27.480 回答