无法使用 c++ 中的类成员函数创建/写入文件
Unable to create/write to a file using class member function in c++
This is 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 : nn";
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 Expiryn";
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 openingn";
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 foundn";
}
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(( 创建一个新的药物文件?我应该怎么做才能创建一个包含所有详细信息的新药物文件,以便我以后可以访问它。
好吧,
似乎我解决了它。如果我对createFile((函数进行微小的更改,程序可以完美运行。
void Medicine::createFile()
{
std::ofstream medicine_file(file_name);
if (!medicine_file.is_open())
std::cout << "file is not openingn";
medicine_file.close();
}
我将第一行从"medicine_file.open(file_name("更改为"std::ofstream medicine_file(file_name(",它按预期运行。
1( 默认fstream
模式不会创建新文件。
`std::fstream medicine_filename;
...
medicine_file.open()'
open(( 不会创建新文件,因为默认模式是 in | out
。要先创建文件,只需指定out
模式。
2( medicine_filename = "药品\" + medicine_name + ".txt";
检查"药物"目录是否存在。要进行测试,请尝试使用驱动器形成完整路径,例如"C:\\Temp\\test.txt"。
相关文章:
- 如何创建函数管道,以便函数一个接一个地运行?
- 如何创建一个对象创建函数,该函数将由与其关联的名称调用?
- 创建函数变体向量时"No matching function for call"错误
- C++ 在映射中创建函数指针向量
- 以编程方式在 C++ 中创建函数
- 如何创建函数可能使用自定义可比C++?
- 编译错误,未创建函数/方法! 对于 brms 模型
- Cython 创建 C 函数别名
- 创建函数指针数组
- 动态创建函数并获取指针
- 混合模板类型的模板参数包(std::使用可选参数重新创建函数)
- 如何创建函数原型命名空间
- 无法创建函数数组
- 在为STD模板函数创建函数对象时,还要还原样板
- 是否可以在C++中声明基类,以便只能通过创建函数创建从它派生的类
- 将原始指针移动到已创建函数中的下一个字符
- qjsonObject如何创建函数
- 尝试在别人的代码中C++创建函数(A* 搜索)
- 在C++中通过模板包重新创建函数签名和调用
- 创建函数时接收"expected unqualified-id"