Code:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
#include <windows.h>
using namespace std;
inline void eatline() { while (cin.get() != '\n') continue; }
struct dog
{
char name[20]; //name
double weight; //weight
double b; //breed
};
const char * file = "dogs.dat";
int main ()
{
dog d1;
cout << fixed << right;
//show initial contents
ifstream fin;
fin.open(file, ios::in |ios::binary);
//NOTE: Some Systems don't allow ios::binary
if (fin.is_open())
{
cout << "Here are the current contents of the" << file << " file:\n";
while (fin.read((char *) &d1, sizeof d1))
{
cout << setw(20) << d1.name << ": "
<< setprecision(0) << setw(12) << d1.weight
<< setprecision(2) << setw(6) << d1.b << "\n";
}
}
fin.close();
//add new data
ofstream fout(file, ios::out | ios::app | ios::binary);
//Note: Some systems don't allow ios::binary
if (!fout.is_open())
{
cerr << "Can't open " << file << " file for output:\n";
exit(1);
}
cout << "Enter dogs name (enter blank line to quit): \n";
cin.get(d1.name, 20);
while (d1.name[0] != '\0')
{
eatline();
cout << "Enter dogs weight: ";
cin >> d1.weight;
cout << "Enter breed: ";
cin >> d1.b;
eatline();
fout.write((char *) &d1, sizeof d1);
cout << "Enter dogs name (Blank line to quit):\n";
cin.get(d1.name, 20);
}
fout.close();
//show revised
fin.clear();
fin.open(ios::in | ios::binary);
if (fin.is_open())
{
cout << "Here is the new contents of the " << file << "file:\n";
while (fin.read((char *) &d1, sizeof d1))
{
cout << setw(20) << d1.name << ": "
<< setprecision(0) << setw(12) << d1.weight
<< setprecision(2) << setw(6) << d1.b << "\n";
}
}
fin.close();
cout << "Done.\n";
return 0;
}
i get 2 errors:
line 65 c:/mydocu~1/c__~1/classes/database.cpp
no matching function for call to `
and
line 362 c:/mingw/include/c__~1/3.2/fstream
candidates are: void
respond A.S.A.P thank you!