C++ c++;访问错误读取

C++ c++;访问错误读取,c++,C++,我在用oku oku(); 我分享了我的项目 错误在void oku()处,因为当我不使用它时,不会出现任何错误。 如果我使用oku();当程序完成时,我出现了一个访问错误读取错误 \u Pnext,0xA3EF54您的struct kimlik具有类型为std::string的成员。您不能简单地将文件中的数据读入持有std::string的struct,因为std::string在内部管理它的数据。根据实现的不同,std::string有一个指针,指向一些数据和数据大小和字符串长度的成员。您

我在用oku

oku();
我分享了我的项目 错误在void oku()处,因为当我不使用它时,不会出现任何错误。 如果我使用oku();当程序完成时,我出现了一个访问错误读取错误


\u Pnext,0xA3EF54

您的
struct kimlik
具有类型为
std::string
的成员。您不能简单地将文件中的数据读入持有
std::string
struct
,因为
std::string
在内部管理它的数据。根据实现的不同,
std::string
有一个指针,指向一些数据和数据大小和字符串长度的成员。您的代码只是简单地写入这些值,从而创建损坏的
std::string
s,指针悬空,内部状态无效。您需要使用
流运算符独立地写入和读取每个字符串

最好将您的
struct
a
class
,并在其中创建
writeToStream()
readFromStream()
方法:

}
然后在适用的情况下使用这些方法:

class Kimlik {
public:
    // constructor etc.

    bool readFromStream(std::istream& s) {
        // implement reading of each member here
        // return true or false depending on whether reading succeeded or not
    }

    bool writeToStream(std::ostream& s) {
        // implement writing of each member here
        // return true or false depending on whether writing succeeded or not
    }

private:
    int          id;
    std::string  TC;
    std::string  Ad;
    std::string  Soyad;
    bool         cinsiyet;
    std::string  dogumgunu;
    std::string  tel;
    std::string  eposta;
    std::string  girist;
    std::string  cıkıst;
    int          depozito;
    int          odenmis_k[12];
    double       endeks[12];
    double       fatura[12];
    int          oda_no;
};

另请参见

您从不检查文件的打开是否成功。我从那里开始。还有-什么是
kisi
?你确定像你这样写是安全的吗?什么是
kisi[i]
?我敢打赌,这是一种内存无法直接安全写入的类型。首先逐行调试它,并找到产生错误的那一行。然后发布整个代码。例如,什么是kisi?@atterson发布整个代码通常是个坏主意。相反,OP应该把重点放在发布一篇文章上,但他们现在还没有做到。是的,我的意思是,有足够的代码来真正理解。。。当然不是整个项目。正确的。
oku();
donus:
system("cls");
int i = 1;
int KB_code = 0;

string menu[7] = { " ","Kisi ekle","Kisi sil","Kisi duzenle","Listeleme 
1","Listeme 2","Cikis" };



while ((KB_code != KB_ESCAPE) && (KB_code != KB_ENTER))
{
    system("cls");
    for (int c = 1; c <= 6; c++) {
        if (c == i)
            cout << ">> " << menu[c] << endl;
        else
            cout << "   " << menu[c] << endl;

    }

    KB_code = _getch();


    switch (KB_code)
    {
    case KB_DOWN:
        i++;
        if (i == 7)
            i = 1;
        cout << endl << i << endl;
        break;



    case KB_UP:
        i = i - 1;
        if (i == 0)
            i = 6;

        cout << endl << i << endl;
        break;

    }


}
if (KB_code == KB_ESCAPE)
    goto bitir;

switch (i)
{
case 1:
    system("cls");
    kisi_ekle();
    break;
case 2:
    cout << "case2";
    break;
case 3:
    cout << "case3";
    break;
case 4:
    system("cls");
    listeleme1();
    break;
case 5:
    cout << "case5";
    break;
default:
    goto bitir;
    break;
}
_getch();
goto donus;
bitir:
system("pause");
}
class Kimlik {
public:
    // constructor etc.

    bool readFromStream(std::istream& s) {
        // implement reading of each member here
        // return true or false depending on whether reading succeeded or not
    }

    bool writeToStream(std::ostream& s) {
        // implement writing of each member here
        // return true or false depending on whether writing succeeded or not
    }

private:
    int          id;
    std::string  TC;
    std::string  Ad;
    std::string  Soyad;
    bool         cinsiyet;
    std::string  dogumgunu;
    std::string  tel;
    std::string  eposta;
    std::string  girist;
    std::string  cıkıst;
    int          depozito;
    int          odenmis_k[12];
    double       endeks[12];
    double       fatura[12];
    int          oda_no;
};
kisi[i].readFromStream(file2);