我有一个简单的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";
}
}
答案 0 :(得分:1)
您的文件位于bin
目录中;检查一下。
因为没有给出文件路径,这意味着默认情况下会在bin
目录中创建它。
要将其保存在所需目录中,您需要提供所需的目录路径。
答案 1 :(得分:0)
建议:执行以下所有操作,然后省略阶段:
1)直接从main调用SavePerson
,以确保它被调用。
2)为文件名提供完整路径,例如c:\\file.txt
。
3)尝试使用与.file
不同的扩展名,例如.txt
。