我有一个很大的患者数据库文件,必须阅读并对其进行操作。我使用下面的代码来做到这一点,方法是将db_nm
设置为文件名./testing.db
,然后调用成员函数read_db。
但是我犯了一些我无法识别的错误。我得到:std::cout << "Error:Unable to read the DB file..." << std::endl;
,因为该程序似乎无法打开我的文件进行读取。
我在做什么错?我是CPP新手。
谢谢。维克
#include <iostream>
#include <string>
#include <stdexcept>
#include <fstream>
#include <utility>
class PatientData {
private:
std::string db_file;
public:
PatientData(std::string db_file);
void read_db(void);
};
PatientData::PatientData(std::string db_file) {
db_file = db_file;
}
void PatientData::read_db(void) {
std::string db = db_file;
std::fstream fs;
std::string line;
fs.open(db, std::ios_base::in);
if (!fs) {
std::cout << "Error:Unable to read the DB file..." << std::endl;
} else {
std::cout << "Processing DB file " << db << "..." << std::endl;
}
fs.close();
}
int main() {
std::string db_nm = "./testing.db";
PatientData ptdb(db_nm);
ptdb.read_db();
return 0;
}
fs.open(db, std::ios_base::in);
if (!fs.is_open()) {
std::cout << "Error:Unable to read the DB file..." << std::endl;
} else {
std::cout << "Processing DB file " << db << "..." << std::endl;
}
fs.close();