无法使用 c++ 中的类成员函数创建/写入文件

Unable to create/write to a file using class member function in c++

本文关键字:创建 函数 文件 成员 c++      更新时间:2023-10-16

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"。