简体   繁体   English

Ifstream读取功能未加载到向量中

[英]Ifstream read function doesn't load into vector

I'm somewhat new to programming, so I'm not sure how to search for this problem, and I know I asked 2 other questions about this, but I can't seem to make it work. 我是编程的新手,所以我不确定如何搜索此问题,并且我知道我还问了另外两个问题,但似乎无法使其工作。

I got a problem, where I have a vector: 我遇到一个问题,在这里我有一个向量:

vector<Device*> Devicelist_;

Whhere I try to load Devices into using this function (I already made a Save-function, which works): 在尝试加载设备以使用此功能的地方(我已经做了一个保存功能,可以使用):

bool Devicelist::LoadFromFile() //Opdaterer vector<Device> fra fil
{
    ifstream LoadFile("Devices.dat", ios::in | ios::binary);

    if (!LoadFile)
    {
        cerr << "File could not be opened." << endl;
        return false;
    }

    LoadFile.seekg(0, ios::end);
    int numberOfDevices = LoadFile.tellg() / sizeof(Device);

    for (int i = 0; i < numberOfDevices; i++)
    {   
        Devicelist_.push_back(new Device);
        LoadFile.read(reinterpret_cast<char *>(Devicelist_[i]), sizeof(Device));
    }

    cout << Devicelist_[0]->getName() << endl;

    LoadFile.close();
    return true;
}

The problem is that LoadFile.read() does not load any Devices into the devicelist. 问题在于LoadFile.read()不会将任何设备加载到设备列表中。

Can you see what my problem is? 你能看到我的问题是什么吗? Thanks in advance. 提前致谢。

Your problem is actually really simple. 您的问题实际上非常简单。 You forgot to reset your get position: 您忘记了重置职位:

LoadFile.seekg(0, ios::end);
int numberOfDevices = LoadFile.tellg() / sizeof(Device);

for (int i = 0; i < numberOfDevices; i++)

should be 应该

LoadFile.seekg(0, ios::end);
int numberOfDevices = LoadFile.tellg() / sizeof(Device);
LoadFile.seekg(0L, ios::beg);
for (int i = 0; i < numberOfDevices; i++)

An alternative to finding the number is using stat: 查找数字的另一种方法是使用stat:

#include <sys/stat.h>
int getNumberOfDevices(char *filename)
{
    struct stat st;
    return st.st_size / sizeof(Device);
}

or, if you wanted to avoid stat, you could do something like this: 或者,如果您想避免统计信息,则可以执行以下操作:

bool Devicelist::LoadFromFile() //Opdaterer vector<Device> fra fil
{
    ifstream LoadFile("Devices.dat", ios::in | ios::binary);
    if (!LoadFile)
    {
        cerr << "File could not be opened." << endl;
        return false;
    }
    int numberOfDevices = 0;
    while (true)
    {   
        Device *tmp = new device; 
        LoadFile.read(reinterpret_cast<char *>(tmp), sizeof(Device));
        if (LoadFile.good()) //we successfully read one
        {
            ++numberOfDevices;
            Devicelist_.push_back(tmp);
        }
        else break; //get out of the infinite loop
    }
    cout << Devicelist_[0]->getName() << endl;
    LoadFile.close();
    return true;
}

This way, it reads all of them, without messing around with positions, and keeps a count when it is finished. 这样,它可以读取所有信息,而不会弄乱位置,并在完成时保持计数。

This line 这条线

LoadFile.seekg(0, ios::end);

puts the file at the end of the file. 将文件放在文件末尾。 You need to put it back at the start by 您需要先将其放回

LoadFile.seekg(0, ios::beg);

Update 更新资料

You can make the code simpler by saving the number of Device s at the top of the file. 通过将Device的数量保存在文件顶部,可以使代码更简单。 Then, you can use: 然后,您可以使用:

int numberOfDevices = 0;
LoadFile.read(&numberOfDevies, sizeof(int));

for (int i = 0; i < numberOfDevices; i++)

There won't be any need to get the size of the file to deduce the number of Device s. 无需获取文件大小即可推断Device的数量。

The other alternative is the one suggested by @phyrrus9 where you keep reading the Device s until there are no more Device s to read from the file. 另一种选择是一个由@ phyrrus9建议您储存读取Device s,至不再有Device s到从文件中读取。

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

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