【发布时间】:2018-09-20 04:21:40
【问题描述】:
这是 MedClass.cpp :-
#include "MedClass.h"
std::string Medicine::getFileName()
{
return file_name;
}
std::string Medicine::getMedicineName()
{
return med_name;
}
std::string Medicine::getMedicineFormula()
{
return med_formula;
}
void Medicine::displayMedInfo()
{
std::cout << "Details of selected medicine : \n\n";
std::cout << "Medicine name :- " << med_name << std::endl;
std::cout << "Medicine formula :- " << med_formula << std::endl;
std::cout << "Medicine weight :- " << med_weight << std::endl;
std::cout << "Medicine Price :- " << med_price << std::endl;
std::cout << "Medicine Expiry Date :- " << expiry_date << std::endl;
std::cout << "Meicine Comapny :- " << med_company << std::endl;
}
int Medicine::getMedUnitsLeft()
{
return med_units_left;
}
void Medicine::writeToFile()
{
medicine_file.open(file_name);
std::cout << "Enter details of medicine in following format :- \n";
std::cout << "Name Formula Weight Price Company Expiry\n";
getline(std::cin, file_input);
medicine_file << file_input;
medicine_file.close();
std::cout<<"File input validated : \n";
}
void Medicine::getFromFile()
{
medicine_file.open(file_name);
medicine_file >>
med_name>>med_formula>>med_weight>>med_price>>med_company>>expiry_date;
medicine_file.close();
}
void Medicine::createFile()
{
medicine_file.open(file_name);
if (!medicine_file.is_open())
std::cout << "file is not opening\n";
medicine_file.close();
}
void Medicine::setFileName(std::string &recieved)
{
file_name = recieved;
}
这是 MedClass.h :-
#pragma once
#include <string>
#include <iostream>
#include <fstream>
#include <limits>
class Medicine
{
public :
std::string getFileName();
std::string getMedicineName();
std::string getMedicineFormula();
void displayMedInfo();
int getMedUnitsLeft();
void writeToFile();
void getFromFile();
void createFile();
void setFileName(std::string &recieved);
private:
std::fstream medicine_file;
std::string file_name;
std::string file_input;
std::string med_name;
std::string med_formula;
std::string expiry_date;
std::string med_company;
float med_weight;
float med_price;
int med_units_left;
};
现在,如果我以这种方式在 main.cpp 中实现它:-
void ViewMedicine()
{
Medicine med;
std::string medicine_name;
std::string medicine_filename;
std::cout << "Enter medicine name to view : \n";
getline(std::cin, medicine_name);
medicine_filename = "Medicines\\" + medicine_name + ".txt";
std::ifstream check_med_by_name;
check_med_by_name.open(medicine_filename);
if (!check_med_by_name.is_open())
{
std::cout << "Medicine not found\n";
}
else
{
med.setFileName(medicine_filename);
med.getFromFile();
med.displayMedInfo();
check_med_by_name.close();
}
}
int main()
{
std::string medicine_name;
std::string medicine_filename;
Medicine medic;
std::cout<<"View Existing Medicines \n";
ViewMedicines();
std::cout<<"Enter the name of medicine to add : \n";
getline(std::cin,medicine_name);
medicine_filename = "Medicines\\" + medicine_name + ".txt";
medic.setFileName(medicine_filename);
medic.createFile();
medic.writeToFile();
return 0;
}
如果程序运行正确,它应该在药品文件夹中创建一个文件,并且不应该在输出中显示“文件未打开”。但是我得到“文件未打开”作为程序输出。这表明文件未创建。即使手动创建药物文件夹也无济于事。
但我可以在医学文件夹中查看已经存在的手动创建的名为 med 的医学文件。
程序输出是这样的:-
View existing medicines
Enter medicine name to view :
med
Details of selected medicine :
Medicine name :- name
Medicine formula :- formula
Medicine weight :- 50
Medicine Price :- 100
Medicine Expire Date :- 10/04/21
Medicine Company :- company
Enter the name of medicine to add :
new
file is not opening
Enter details of medicine in following format :-
Name Formula Weight Price Company Expiry
new new 50 100 new 0
file input validated
press any key to continue...
那么为什么我能够查看已经存在的药物文件,但不能使用成员函数 CreateFile() 创建一个新的?我应该怎么做才能创建一个包含所有详细信息的新药物文件,以便我以后可以访问它.
【问题讨论】:
-
你试过调试你的代码吗?你检查过
open函数(使用默认参数)的行为吗?
标签: c++ class fstream file-handling