#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
您的struct kimlik
具有类型为std::string
的成员。你不能简单地将数据从文件读取到struct
保存std::string
,因为std::string
内部管理它的数据。根据实现的不同,std::string
具有指向某些数据和成员的指针,用于数据大小和字符串长度。您的代码只是写入这些值,从而创建带有悬空指针和无效内部状态的损坏std::string
。您需要使用<<
和>>
流运算符独立写入和读取每个字符串。
最好将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);
另请参阅如何读取和写入 STL C++字符串?