我正在尝试从文件中读取我的数据库。
这是我的 save_base 方法:
void data_base::save_base()
{
fstream file;
file.open("base.dat", ios::in | ios::out | ios::trunc);
if(file.good()==true) {
node *p = new node();
p=first;
while(p) {
file << p->content->connect() << ";" << "\n";
p=p->next;
}
file.close();
}else{
cout << "Err - opening file." << endl;
}
}
连接方法:
string product::connect() {
ostringstream do_string;
do_string << lp;
string new_lp = do_string.str();
ostringstream do_string1;
do_string1 << count;
string new_count = do_string1.str();
ostringstream do_string2;
do_string2 << prize;
string new_prize = do_string2.str();
ostringstream do_string3;
do_string3 << vat;
string new_vat = do_string3.str();
string connected = type + ";" + new_lp + ";" + name + ";" + new_count + ";" + unit + ";" + new_prize + ";" + new_vat;
return connected;
}
和 read_base 方法:
void data_base::read_base()
{
fstream file;
file.open("base.dat", ios::in);
if(file.good()==true)
{
char data_row[50];
int i=1;
while(!file.eof()) {
file.getline(data_row,100);
string data_content[50];
int j = 0;
char *buff;
buff = strtok (data_row,";");
while (buff != NULL) {
data_content[j] = buff;
buff = strtok (NULL, ";");
j++;
}
string type = data_content[0];
int lp;
istringstream iss1(data_content[1]);
iss1 >> lp;
double count;
istringstream iss2(data_content[3]);
iss2 >> count;
double prize;
istringstream iss3(data_content[5]);
iss3 >> prize;
double vat;
istringstream iss4(data_content[5]);
iss4 >> vat;
// Sprawdzamy typ obiektu zapisanego w danym wierszu pliku
if(type == "product")
{
product new_prod(lp, data_content[2], count, data_content[4], prize, vat);
product *new_product = new product(new_prod);
this->add(new_product);
}
i++;
}
file.close();
}else{
cout << "Err opening file." << endl;
}
}
我正在向数据库中添加一些产品,它工作正常。即使保存到文件也很好。但主要问题是当我试图从文件中读取数据库时。从文件中读取数据库工作正常,但最后,应用程序不会自行结束。我认为仍有一些缓冲区需要关闭。但我不知道它们是哪个或多近。