Reputation: 1815
I have a simple I/O program, but when I save a file it is not being created in the project directory or anywhere else on my computer. It all compiles fine, all the functions work, but upon loading I receive a blank file. How can I fix this?
I'm using Xcode also.
#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";
}
}
Upvotes: 1
Views: 3159
Reputation: 17655
Your file is in the bin
directory; check there.
Because you are not given the file path that means it is created by default in the bin
directory.
To save it in your required directory, you need to give your required directory path.
Upvotes: 1
Reputation: 13510
Suggestions: Do all the following, then omit stages:
1) Call SavePerson
directly from main, to make sure it's get called.
2) Give the file name a full path, like c:\\file.txt
.
3) Try a different extension than .file
, like, for example, .txt
.
Upvotes: 0