如何在c ++中检测文件是否为空?

时间:2015-10-23 13:59:27

标签: c++ c++11

我正在尝试在程序加载时从文件加载数据,但是我需要首先检测文件是否为空? 当它加载时它使程序无限,那么如何在c ++中检测空文件?

void loadfromfile(record *&newrecord, ifstream &in)
{
    in.open("records.txt", ios::binary | ios::in);
    record *temprecord = new record;
    record *tempreholder = new record;
    tempreholder = newrecord;
    while (!in.eof())
    {
        if (newrecord == nullptr)
        {
            newrecord = new record;
            in.read((char *)(&newrecord), sizeof(newrecord));
            newrecord->node = nullptr;
        }
        else
        {
            in.read((char *)(&temprecord), sizeof(temprecord));
            temprecord->node = nullptr;
            newrecord->node = temprecord;
        }
    }
    newrecord = tempreholder;
    in.close();
}

0 个答案:

没有答案