【问题标题】:c++ access error readingc++访问错误读取
【发布时间】:2018-10-28 03:18:52
【问题描述】:
#include<iostream>
using namespace std;
#include<conio.h>
#include<string>
#include <fstream>
#include <time.h>    


#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_ESCAPE 27
#define KB_ENTER 13


struct kimlik {
int id;
string TC;
string Ad;
string Soyad;
bool cinsiyet;
string dogumgunu;
string tel;
string eposta;
string girist;
string cıkıst;
int depozito;
int odenmis_k[12];
double endeks[12];
double fatura[12];
int oda_no;
};
struct kimlik kisi[20];

kisi 是结构 ^ 我认为错误在这一行下

void oku() {
cout << "okuma islemi yapiliyor";
ifstream file2;
file2.open("kisi.txt", ios::in |  ios::binary );
file2.seekg(0L, ios::beg);

for (int i = 0; i < 20; i++) {
int i = 0;
    cout << "suanda cursor " << file2.tellg() << endl;


    file2.read((char *)&kisi[i], sizeof(kisi[i]));


}

我关闭文件

file2.close();
cout << "Islem tamamlandi";
getchar();
}



int main() {

如果使用不使用 oku() 没有任何错误

oku();

我正在使用 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");

程序在这里工作,我按任意键关闭控制台出现错误

}

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

_Pnext,0xA3EF54

【问题讨论】:

  • 你永远不会检查文件的打开是否成功。我会从那里开始。另外 - kisi 是什么?你确定像你一样写信是安全的吗?
  • 什么是kisi[i]?我敢打赌,这是一种你不能安全地直接写入内存的类型。
  • 先逐行调试,找到产生错误的那一行。然后发布整个代码。例如kisi是什么?
  • @Attersson 发布整个代码通常是个坏主意。相反,OP 应该专注于发布 MCVE,他们目前没有这样做
  • 是的,我的意思是足够多的代码才能真正有意义......当然不是整个项目。对。

标签: c++


【解决方案1】:

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

最好将struct 设为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[i].readFromStream(file2);

另见How to read and write a STL C++ string?

【讨论】:

    猜你喜欢
    • 2013-08-12
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多