简体   繁体   English

需要有关通过fstream保存变量的帮助,我是否需要使用向量?

[英]Need help regarding saving variables via fstream, do I need to use vector?

I doing this project, where I want to save some variables for a device; 我正在做这个项目,我想在其中保存设备的一些变量。 Devicename, ID and type. 设备名称,ID和类型。

bool Enhedsliste::newDevice(string deviceName, string type)
{
fstream myFile;
string line;
char idDest[4];

myFile.open("Devices.txt", ios::app | ios::in | ios::out); //Opretter/Åbner fil, app = startlinje er den nederste linje, in out = input output
if (myFile.is_open())
{
    while (getline(myFile, line)) //Går filen igennem for hver linje
    {
        if (line == deviceName)
        {
            cout << deviceName << "-device already exists." << endl;
            return false;
        }
    }
}
else
{
    cout << "Uable to open file." << endl;
    return false;
}
myFile.close();

myFile.open("Devices.txt", ios::app | ios::in | ios::out); //Opretter/Åbner fil, app = startlinje er den nederste linje, in out = input output
if (myFile.is_open())
{
    if (type == "Lampe")
        type_ = 1;
    else if (type == "Roegalarm")
        type_ = 2;
    else if (type == "Tyverialarm")
        type_ = 3;
    else
    {
        cout << "Type does not exists." << endl;
        return false;
    }

    deviceName_ = deviceName;
    myFile << deviceName_ << endl;

    id_++;
    sprintf_s(idDest, "%03d", id_);
    myFile << idDest << endl;

    myFile << type_ << endl;

    myFile.close();

    return true;
}
else
{
    cout << "Uable to open file." << endl;
    return false;
}
}

Now I also want to make a deleteDevice, where I can take deviceName as a parameter and it will find that line and delete ID and type, but I have no idea on how to do this. 现在,我还想创建一个deleteDevice,在这里我可以将deviceName作为参数,它将找到该行并删除ID和类型,但是我不知道如何执行此操作。

Do I need to rewrite my addDevice to vector? 我需要将addDevice重写为vector吗? And how do I do this? 我该怎么做?

Thanks in advance and sorry for bad code, explanation etc. I'm new to this. 在此先感谢您,并感谢您提供错误的代码,解释等。对此我是陌生的。

To delete a line from your current text file, you will have to read in all lines, eg storing the data in a std::map , remove the relevant item, and write it all back out again. 要从当前文本文件中删除一行,您将必须读入所有行,例如,将数据存储在std::map ,删除相关项,然后再次全部写回。 An alternative is to use a database or binary fixed size record file, but reading everything into memory is common. 一种替代方法是使用数据库或固定大小的二进制记录文件,但是将所有内容读取到内存中是很常见的。 By the way, I would remove the ios::app append mode for opening the file for reading. 顺便说一句,我将删除ios::app追加模式以打开文件进行读取。 It translates to the equivalent of append mode for fopen , but the C99 standard is unclear on what it means for reading, if anything. 它相当于fopen的append模式,但是C99标准尚不清楚读取的含义(如有)。

To delete a single device you might read the file and write to a temporary file. 要删除单个设备,您可以读取文件并写入临时文件。 After transferring/filtering the data, rename and delete files: 传输/过滤数据后,重命名和删除文件:

#include <cstdio>
#include <fstream>
#include <iostream>

int main() {
    std::string remove_device = "Remove";
    std::ifstream in("Devices.txt");
    if( ! in) std::cerr << "Missing File\n";
    else {
        std::ofstream out("Devices.tmp");
        if( ! out) std::cerr << "Unable to create file\n";
        else {
            std::string device;
            std::string id;
            std::string type;
            while(out && std::getline(in, device) && std::getline(in, id) && std::getline(in, type)) {
                if(device != remove_device) {
                    out << device << '\n' << id << '\n' << type << '\n';
                }
            }
            if( ! in.eof() || ! out) std::cerr << "Update failure\n";
            else {
                in.close();
                out.close();
                if( ! (std::rename("Devices.txt", "Devices.old") == 0
                && std::rename("Devices.tmp", "Devices.txt") == 0
                && std::remove("Devices.old") == 0))
                    std::cerr << "Unable to rename/remove file`\n";
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM