Error when loading int and strings from .dat file

So I'm trying to load account information for a banking program I'm attempting to make. Every time I run the code I get thi serror message:
Unhandled exception thrown: read access violation.
_Pnext was 0x2D3124.

and it links me to this section of xmemory

1
2
3
4
5
6
7
8
9
10
11
12
  #if _ITERATOR_DEBUG_LEVEL == 2
    if (_Myproxy) { // proxy allocated, drain it
        _Lockit _Lock(_LOCK_DEBUG);

        for (auto _Pnext = &_Myproxy->_Myfirstiter; *_Pnext; *_Pnext = (*_Pnext)->_Mynextiter) { // Error message here
            (*_Pnext)->_Myproxy = nullptr;
        }

        _Myproxy->_Myfirstiter = nullptr;
    }
#endif // _ITERATOR_DEBUG_LEVEL == 2
}


I'm not so sure what I'm doing wrong. this is the part of my code that handles the writing of a .dat file and loading along with where I input the account information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
void accountData::writeAccount()
{
	accountData ac;
	ofstream outFile;
	outFile.open("account_information.dat", ios::binary | ios::app); // ios::binary = opens text document in binary format instead of translating to .txt | ios::app adds new data to the end of the document
	ac.customerInfo();
	outFile.write(reinterpret_cast<char*>(&ac), sizeof(accountData)); // reinterpret_cast converts a pointer type
	outFile.close();
}


void accountData::customerInfo()
{
	cin.clear();
	cin.sync();

	//------------------------------ACCOUNT NUMBER------------------------------
	system("CLS");
	cout << "Enter available account number: ";
	cin >> accountNumber;

	// -------------------------------NAME---------------------------------------
	system("CLS");
	cout << "Customer Full name: ";
	cin.ignore();
	getline(cin,name);
	system("CLS");

	//------------------------------DATE OF BIRTH--------------------------------
	cout << "Enter Birth Month (MM):";
	cin >> MM;
	cout << "\nEnter birth Day (DD): ";
	cin >> DD;
	cout << "\nEnter Birth Year (YYYY): ";
	cin >> YYYY;

	system("CLS");

	//n = sprintf_s(DOB, "%d/%d/%d", MM, DD, YYYY);
	//printf("%s\n", DOB, n);


	//------------------------------Address-------------------------------------
	system("CLS");
	cout << "Enter the customers full address: ";
	cin.ignore();
	getline(cin, address);

	//------------------------------Phone #-------------------------------------
	system("CLS");
	cout << "Enter the customers Phone number: ";
	getline(cin, phoneNumber);

	//------------------------------SSN-----------------------------------------
	system("CLS");
	cout << "Enter the customers Social Security Number: ";
	getline(cin, SSN);


	system("CLS");

	//cout << "Customer Info: " << name  << endl << address << endl << phoneNumber << endl << SSN << endl;
	//printf("%s\n", DOB, n);



}

//Loads Account information
void accountData::allAccounts()
{
	accountData ac;
	ifstream inFile;
	inFile.open("account_information.dat", ios::binary);
	if (!inFile)
	{
		cout << "No Data available!\n";
		return;
	}
	cout << "====================-HOLDER LIST-======================\n";
	while (inFile.read(reinterpret_cast<char*> (&ac), sizeof(accountData)))
	{
		ac.dataBase();
	}
	inFile.close();
}


> accountData ac;
...
> outFile.write(reinterpret_cast<char*>(&ac), sizeof(accountData));
What is an accountData ?

If you have any of the following in your struct/class, then it's not going to work.
- pointers
- constructors
- destructor
- inheritance
- virtual methods
- members which themselves have any of the above.

https://en.wikipedia.org/wiki/Passive_data_structure

This would work
1
2
3
struct accountData {
    char name[10];
};


These would not.
1
2
3
struct accountData {
    char *name;
};


1
2
3
struct accountData {
    std::string name;
};


Shouldn't this:
for (auto _Pnext = &_Myproxy->_Myfirstiter; *_Pnext; *_Pnext = (*_Pnext)->_Mynextiter)
be this?
for (auto _Pnext = &_Myproxy->_Myfirstiter; *_Pnext; _Pnext = &(*_Pnext)->_Mynextiter)

Also, I'm pretty sure that identifiers beginning with underscore (_) are reserved for the compilation system. So nearly all of your identifiers are not a potential problem. Someone correct me if I'm wrong.
@dhayden, I understood that to be all library code.

It's just where the program barfed, and the first source code the debugger found walking up the stack.

The OP needs to keep walking up the stack until they get to their code.
Ok so after reading @salem c's post I learned that the problem was being caused by me using strings to take in a couple inputs. I changed them all to char sample[xx] and that fixed the problem.

This is what it looks like now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class accountData
{
public:

	void writeAccount();
	void customerInfo();
	void balanceInquiry();
	void allAccounts();
	void dataBase();

	ofstream outFile;

	char name[50];
	int MM;
	int DD;
	int YYYY;
	char DOB[50];
	int n;
	char address[100];
	char phoneNumber[20];
	char SSN[15];
	int accountNumber;
	
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "AccountData.h"
#include <iostream>
#include<fstream>
#include <string>
#include <stdio.h>
#include <iomanip>

using namespace std;

void accountData::writeAccount()
{
	accountData ac;
	ofstream outFile;
	outFile.open("account_information.dat", ios::binary | ios::app);
	ac.customerInfo();
	outFile.write(reinterpret_cast<char*>(&ac), sizeof(accountData));
	outFile.close();
}

void accountData::customerInfo()
{
	cin.clear();
	cin.sync();

	//------------------------------ACCOUNT NUMBER------------------------------
	system("CLS");
	cout << "Enter available account number: ";
	cin >> accountNumber;

	// -------------------------------NAME---------------------------------------
	system("CLS");
	cout << "Customer Full name: ";
	cin.ignore(); 
	cin.get(name, 50);
	cout << name << endl;
	system("pause");
	system("CLS");

	//------------------------------DATE OF BIRTH--------------------------------
	cout << "Enter Birth Month (MM):";
	cin >> MM;
	cout << "\nEnter birth Day (DD): ";
	cin >> DD;
	cout << "\nEnter Birth Year (YYYY): ";
	cin >> YYYY;

	system("CLS");

	//n = sprintf_s(DOB, "%d/%d/%d", MM, DD, YYYY);
	//printf("%s\n", DOB, n);



	//------------------------------Address-------------------------------------
	system("CLS");
	cout << "Enter the customers full address: ";
	cin.ignore();
	cin.get(address, 100);
	cout << endl << address;
	system("pause");

	//------------------------------Phone #-------------------------------------
	system("CLS");
	cout << "Enter the customers Phone number: ";
	cin.ignore();
	cin.get(phoneNumber, 20);
	cout << endl << phoneNumber;
	system("pause");

	//------------------------------SSN-----------------------------------------
	system("CLS");
	cout << "Enter the customers Social Security Number: ";
	cin.ignore();
	cin.get(SSN, 15);
	cout << endl << SSN;
	system("pause");


	system("CLS");

	//cout << "Customer Info: " << name  << endl << address << endl << phoneNumber << endl << SSN << endl;
	//printf("%s\n", DOB, n);



}


void accountData::allAccounts()
{
	accountData ac;
	ifstream inFile;
	inFile.open("account_information.dat", ios::binary);
	if (!inFile)
	{
		cout << "No Data available!\n";
		return;
	}
	cout << "====================-HOLDER LIST-======================\n";
	while (inFile.read(reinterpret_cast<char*> (&ac), sizeof(accountData)))
	{
		ac.dataBase();
	}
	inFile.close();
}

void accountData::dataBase()
{
	cout << accountNumber << " " << name << " " << MM << "/" << DD << "/" << YYYY << " " << address << " " << phoneNumber << " "<< SSN << endl;
}

Except, now that it displays the stored information properly, after it closes the file I get this error message
Exception thrown at 0x008F76D2 in BankingSystem_v0.exe: 0xC0000005: Access violation reading location 0x00B39160.

at the end of this line on fstream
virtual __CLR_OR_THIS_CALL ~basic_ofstream() noexcept {}

what am I doing wrong when accessing the reading location?
> outFile.open("account_information.dat", ios::binary | ios::app);
Make sure you don't have the garbage of all the previous failures already in the file.

Seems to work otherwise.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ ./a.out 
Enter available account number: 1234
Customer Full name: Fred Flintstone
Fred Flintstone
Enter Birth Month (MM):1

Enter birth Day (DD): 2

Enter Birth Year (YYYY): 42
Enter the customers full address: Bedrock city

Bedrock cityEnter the customers Phone number: 432123

432123Enter the customers Social Security Number: 4431355

4431355====================-HOLDER LIST-======================
1234 Fred Flintstone 1/2/42 Bedrock city 432123 4431355
I deleted the old file and it still gave me an error code. I even tried opening up a new project on vs on a different computer and pasting the code onto that but it still gives me the same error code for Access "violation reading location 0xFDFD0043"
Can you post a minimal main() showing how you call things.

I just did this.
1
2
3
4
5
int main ( ) {
  accountData ac;
  ac.writeAccount();
  ac.allAccounts();
}
Here is my entire source.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
#include "AccountData.h"

using namespace std;

string g_Dep = "Deposit";
string g_wit = "Withdraw";
string g_balEn = "Balance Enquiry";
string g_all = "All account holder list";
string g_close = "Close an account";
string g_mod = "Modify an account";
string g_create = "Create an account"; // Class Item

string g_cash = "Cash Drawer";

accountData custInfo;


int userAccess(int user)
{
	switch (user)
	{
	case 1: // Manager options
		cout << "\t\t\t1. " << g_Dep << endl;
		cout << "\t\t\t2. " << g_wit << endl;
		cout << "\t\t\t3. " << g_balEn << endl;
		cout << "\t\t\t4. " << g_all << endl;
		cout << "\t\t\t5. " << g_close << endl;
		cout << "\t\t\t6. " << g_mod << endl;
		cout << "\t\t\t7. " << g_create << endl; // Class Item
		break;
	case 2: // Teller options
		cout << "\t\t\t1. " << g_Dep << endl;
		cout << "\t\t\t2. " << g_wit << endl;
		cout << "\t\t\t3. " << g_balEn << endl;
		cout << "\t\t\t4. " << g_all << endl;
		cout << "\t\t\t5. " << g_cash << " FUTURE UPDATE" << endl;
		break;
	case 3: // Customer service options
		cout << "\t\t\t1. " << g_Dep << endl;
		cout << "\t\t\t2. " << g_wit << endl;
		cout << "\t\t\t3. " << g_balEn << endl;
		cout << "\t\t\t4. " << g_all << endl;
		cout << "\t\t\t5. " << g_close << endl;
		cout << "\t\t\t6. " << g_mod << endl;
		cout << "\t\t\t7. " << g_create << endl;
		break;

	}
	return 0;
}

void myPause() // An alternative to system("pause")
{
	cout << "Press ENTER to continue...";
	cin.clear();
	cin.sync();
	cin.get();
}

void main()
{
	system("color a");
	int option1 = 0; // User bank position
	int option2 = 0; // NAVIGATION MENU

	cout << "\t\t============================ \n";
	cout << "\t\t    Banking System      \n";
	cout << "\t\t       by Jason Sanchez\n";
	cout << "\t\t============================ \n";
	myPause();
	system("CLS");
	
	cout << "\t\t++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n";
	cout << "\t\tAre you a: 1.Manager 2.Teller 3.Customer Service rep\n\n";
	cout << "\t\t++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";

	cin >> option1;

	system("CLS");

	bool exit = false;
	while (exit == false)
	{
		cout << "\t\t\t=========================================\n";
		userAccess(option1);
		cout << "\t\t\t=========================================\n";
		cout << "Enter an option: ";
		cin >> option2;



		switch (option2)
		{
		case 7: // Create an account
		{
			custInfo.writeAccount();
			break;
		}
		case 4:
		{
			custInfo.allAccounts();
			break;
		}
		}
	}
	
	
	
	myPause();
}
It was all right until you put this into the class (why is this in the class?)
> ofstream outFile;
Remember the rules - no NON-POD data.

> bool exit = false;
Beware of using common names which are also the names of existing functions.
https://en.cppreference.com/w/c/program/exit

> void main()
main returns an int.

> custInfo.writeAccount();
...
1
2
3
4
5
6
7
8
9
void accountData::writeAccount()
{
	accountData ac;
	ofstream outFile;
	outFile.open("account_information.dat", ios::binary | ios::app);
	ac.customerInfo();
	outFile.write(reinterpret_cast<char*>(&ac), sizeof(accountData));
	outFile.close();
}

You now have THREE outFiles
- the local variable
- the one in the class instance you call 'ac'
- the one in the class instance 'this', also known as custInfo in main()

TBH, if you want to update the class instance in main, you need
1
2
3
4
5
6
7
8
void accountData::writeAccount()
{
	ofstream outFile;
	outFile.open("account_information.dat", ios::binary | ios::app);
	customerInfo();
	outFile.write(reinterpret_cast<char*>(this), sizeof(accountData));
	outFile.close();
}

I was using a sample project on a banking system that I found on the internet and in there they wrote the writeAccount() function the same as I wrote it. The difference is I wrote it using .h and .cpp files while they wrote it all on the source.cpp file
> http://www.cppforschool.com/project/banking-system-project.html
thats the project i'm using for reference

After changing writeAccount() to :
1
2
3
4
5
6
7
8
9
void accountData::writeAccount()
{
	ofstream outFile;
	outFile.open("account_information.dat", ios::binary | ios::app);
	customerInfo();
	outFile.write(reinterpret_cast<char*>(this), sizeof(accountData)); 
	outFile.close();
}


and changing the allAccounts() function same way, the program is now working properly =).
Thank you for helping me out
Topic archived. No new replies allowed.