Error writing a vector to a file
Jun 23, 2018 at 7:30pm UTC
I am trying to learn how to write a vector to a file, then read that vector to from a file. In the code below, I attempted to write to a file but it gives me an error, the error is C3867. I read through the articles and searched but I haven't been able to find anything to help me solve the issue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
struct Person
{
std::string First_Name;
std::string Last_Name;
std::string Phone_Number;
int Age;
}PersonInfo;
std::vector<Person> person;
int howManyPeopleInDataBase;
void Menu();
void addPerson();
void printInfomation();
void saveToFile();
void loadFromFile();
int main()
{
Menu();
}
void Menu()
{
char answer;
do {
std::cout << "Employee Database\n" ;
std::cout << "Amout of employees in Database: " << howManyPeopleInDataBase << std::endl;
std::cout << "1. Add Person" << std::endl;
std::cout << "2. Show All People" << std::endl;
std::cout << "3. Save Information to a File" << std::endl;
std::cout << "4. Load from a File" << std::endl;
answer = _getch();
switch (answer)
{
case '1' :
addPerson();
break ;
case '2' :
printInfomation();
break ;
case '3' :
saveToFile();
break ;
case '4' :
loadFromFile();
break ;
default :
break ;
}
std::cout << "Press enter to continue..." << std::endl;
while (_getch() != 13);
system("cls" );
} while (answer != 27);
}
void addPerson()
{
std::cout << "Enter First Name: " ;
std::cin >> PersonInfo.First_Name;
std::cout << "Enter Last Name: " ;
std::cin >> PersonInfo.Last_Name;
std::cout << "Enter Your Age: " ;
std::cin >> PersonInfo.Age;
std::cout << "Enter Your Phone Number: " ;
std::cin >> PersonInfo.Phone_Number;
std::cin.sync();
howManyPeopleInDataBase++;
}
void printInfomation()
{
if (howManyPeopleInDataBase > 0)
{
for (const Person &i : person)
{
std::cout << "First Name: " << i.First_Name << std::endl;
std::cout << "Last Name: " << i.Last_Name << std::endl;
std::cout << "Age: " << i.Age << std::endl;
std::cout << "Phone Number: " << i.Phone_Number << std::endl;
}
}
else std::cout << "No information int database..." << std::endl;
}
void saveToFile()
{
std::ofstream file("data.txt" );
if (file.is_open)
{
file << howManyPeopleInDataBase;
for (const Person &i : person)
{
file << i.First_Name;
file << i.Last_Name;
file << i.Age;
file << i.Phone_Number;
}
file.close();
}
else std::cout << "Unable to save to database..." ;
}
/*
void loadFromFile()
{
std::ifstream file("data.txt");
if (file.is_open)
{
file >> howManyPeopleInDataBase;
if (howManyPeopleInDataBase > 0)
{
for (const Person &i : person)
{
file >> i.First_Name;
file >> i.Last_Name;
file >> i.Age;
file >> i.Phone_Number;
}
std::cout << "File loaded...";
}
else { std::cout << "Data file is empty.."; }
}
else { std::cout << "File can not be found..."; }
}*/
Jun 23, 2018 at 8:49pm UTC
if (file.is_open)
should be
if (file.is_open())
.
Topic archived. No new replies allowed.