Ifstream read function doesn't load into vector

前端 未结 2 1709
既然无缘
既然无缘 2021-01-26 21:20

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

2条回答
  •  北海茫月
    2021-01-26 21:52

    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 Devices at the top of the file. 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 Devices.

    The other alternative is the one suggested by @phyrrus9 where you keep reading the Devices until there are no more Devices to read from the file.

提交回复
热议问题