1

我有一个简单的 I/O 程序,但是当我保存一个文件时,它并没有在项目目录或我计算机上的其他任何地方创建。一切都编译得很好,所有功能都可以工作,但是在加载时我收到一个空白文件。我怎样才能解决这个问题?

我也在使用 Xcode。

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>

    using namespace std;

    class Person
    {
        public:
            Person(){cout << "\n\tA person has been built. Edit their Info";}
            ~Person();

            void setName(string nameIn){name = nameIn;}
            void setOccupation(string occupationIn){occupation = occupationIn;}
            void setLocation(string locationIn){location = locationIn;}
            void setReferences(string referencesIn){references = referencesIn;}

            string getName(){return name;}
            string getOccupation(){return occupation;}
            string getLocation(){return location;}
            string getReferences(){return references;}

        private:
            string name;
            string occupation;
            string location;
            string references;
    };

    void CreatePerson();
    void EditPerson();
    void DisplayPerson();
    void SavePerson();
    void LoadPerson();

    Person * Pptr;

    int main(void)
    {
        char choice[10];
        Pptr = new Person();

        cout << "\n\tPersonnel Database";

        while (choice[0] != 'q')
        {
            cout << "\n\t--------------MAIN MENU---------";
            cout << "\n\t (C)reate a Person";
            cout << "\n\t (E)dit a Person";
            cout << "\n\t (D)isplay a Perosn";
            cout << "\n\t (S)ave a Perosn";
            cout << "\n\t (L)oad a Person";
            cout << "\n\t (Q)uit";
            cout << "\n\t";

            cin >> choice;

            switch(choice[0])
            {
                case 'c': CreatePerson();break;
                case 'e': EditPerson();break;
                case 'd': DisplayPerson(); break;
                case 's': SavePerson();break;
                case 'l': LoadPerson();break;
                case 'q': cout << "Exiting...";break;
                default: cout <<"\n\tInvalid Entry";
            }
        }
        return EXIT_SUCCESS;
    }

    void CreatePerson()
    {
        Pptr = new Person();

    }

    void EditPerson()
    {
        string tempInfo;

        cout << "\n\tEdit Personnel record";

        cout << "\n\tName: ";
        cin.ignore();
        getline(cin,tempInfo);
        Pptr->setName(tempInfo);

        cout << "\n\tOccupation: ";
        getline(cin,tempInfo);
        Pptr-> setOccupation(tempInfo);

        cout << "\n\tLocation: ";
        getline(cin,tempInfo);
        Pptr->setLocation(tempInfo);

        cout << "\n\tReferences: ";
        getline(cin,tempInfo);
        Pptr->setReferences(tempInfo);
    }

    void DisplayPerson()
    {
        cout << "\n\tPersonnel  Record";
        cout << "\n\tName: " << Pptr->getName();
        cout << "\n\tOccupation: "<< Pptr->getOccupation();
        cout << "\n\tLocation: " << Pptr->getLocation();
        cout << "\n\tReferences: " << Pptr->getReferences();
    }

    void SavePerson()
    {
        try
        {
            ofstream data;
            data.open("personnelData.file",ios::out);

            data << Pptr->getName() << "\n"; // \n is the delimiter
            data << Pptr->getOccupation() << "\n";
            data << Pptr->getLocation()<< "\n";
            data << Pptr->getReferences() << "\n";

            data.close();
            cout << "\n\tSuccessfully Saved";
        }
        catch (exception e) {
            cout << "\n\tcould not save Person.";
        }
    }

    void LoadPerson()
    {
        try
        {
            string tempInfo;
            Pptr = new Person();

            ifstream in;
            in.open("data.txt", ios::in);

            getline(in, tempInfo);
            Pptr->setName(tempInfo);

            getline(in, tempInfo);
            Pptr->setOccupation(tempInfo);

            getline(in, tempInfo);
            Pptr->setLocation(tempInfo);

            getline(in, tempInfo);
            Pptr->setReferences(tempInfo);
        }
        catch (exception e) {
            cout <<"\n\tUnable to load file";
        }
    }
4

2 回答 2

1

您的文件在bin目录中;检查那里。

因为您没有给出文件路径,这意味着它是默认在bin目录中创建的。

要将其保存在所需的目录中,您需要提供所需的目录路径。

于 2012-12-03T05:51:12.333 回答
0

建议:执行以下所有操作,然后省略阶段:

1)SavePerson直接从main调用,以确保它被调用。

2) 给文件名一个完整的路径,比如c:\\file.txt.

3) 尝试使用与 不同的扩展名.file,例如.txt.

于 2012-12-03T06:00:37.350 回答