在C ++

时间:2019-01-20 01:06:48

标签: c++ file object binary

我对以下功能有疑问。问题在于,当到达“返回真”行或while循环的右括号时,将出现错误:xutility内部头文件中的“读取访问冲突。_Pnext为0x6DB24”。我想我的错误在于我如何使用file.read方法,但是我不确定哪种操作正确的方法。

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>

class Profile {
public:
    std::string pass;
    std::string name;
    Profile() {};
    Profile(std::string name, std::string password) {
        this->name = name;
        this->pass = password;
    }
};

bool isLegitProfile(std::string name, std::string password) {
    Profile tempProfile(name, password);
    std::ifstream file("profiles.bin", std::ios::binary | std::ios::in);
    assert(file.is_open(), "Couldn't open file in isLegitProfile function");
    std::streampos end;
    file.seekg(0, std::ios::end);
    end = file.tellg();
    file.seekg(0, std::ios::beg);
    while (file.tellg() != end) {
        Profile temp;
        file.read((char*)&temp, sizeof(temp));
        if (temp.pass == password && temp.name == name) {
            file.close();
            return true;
        }
    }
   file.close()
   return false;
}


int main()
{

    std::string name, pass;
    std::cin >> name;
    std::cin >> pass;
    isLegitProfile(name, pass);

    return 0;
} 

0 个答案:

没有答案