Reading/Writing information to a Binary file

When I run the code it works. But when I "Press any key to continue.." I get the following error message.

Run-Time Check Failure #2 - Stack around the variable 'A' was corrupted.

I'm guessing its referring to my A array of type Student. Don't understand more than that though. Any help on how to resolve this issue, would be appreciated.

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
#include <iostream>
#include <fstream>

using namespace std;

struct Student
{
	char Name[20];
	char ANumber[9];
	int Age;
	float GPA;
};

void studentPrint(Student [], int size);

int main(){

	Student A[10];
	Student C[10];

	for (int i = 1; i <= 10; i++){
		if (i >= 1 && i < 5){
			strcpy_s(A[i].Name, 21, "Rob Smith");
			strcpy_s(A[i].ANumber, 10, "A67241879");
			A[i].Age = 12;
			A[i].GPA = 3.49;
		}
		else{
			strcpy_s(A[i].Name, 21, "John Doe");
			strcpy_s(A[i].ANumber, 10, "A01241239");
			A[i].Age = 17;
			A[i].GPA = 2.90;
		}
	}

	fstream file;
	file.open("Record.bin", ios::out | ios::binary);

	for (int j = 1; j <= 10; j++){
        //If I write (char *) A[j] it says there is no suitable type conversion
	     file.write((char *) &A[j], sizeof(A));
	     //sizeof determines how far your cursor moves
	}
	file.close();

	file.open("Record.bin", ios::in | ios::binary);
	file.clear(); //use before seekg, or seekp to make sure you can move your cursor
	file.seekg(0L, ios::beg);

	for (int k = 1; k <= 10; k++){
		//If I write (char *) C[k] it says there is no suitable type conversion
		file.read((char *) &C[k], sizeof(C));
	}
	studentPrint(C, 10);

	system("pause");
	return 0;
}

void studentPrint(Student a[], int size){
	for (int i = 1; i <= 10; i++){
		cout << "Student Number " << i << "  *****" << endl;
		cout << "Student Name: " << a[i].Name << endl;
		cout << "Student A Number: " << a[i].ANumber << endl;
		cout << "Student Age: " << a[i].Age << endl;
		cout << "Student GPA: " << a[i].GPA << endl;
		cout << endl;
	}
}
I was just trying to run this code to see what it looks like and received an different error. A message 'strcpy_s' is undeclared(first use this function).
Although my code is running without this header file, maybe you need to add #include <string> ? I'm running this code in Microsoft Visual Studio 2013.
Last edited on
You are reading/writing past the bounds of the array for (int i = 1; i <= 10; i++) should be for (int i = 0; i < 10; i++)
Problem solved ! Implemented the changes to the code below, which should be working without any issues.

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
#include <iostream>
#include <fstream>

using namespace std;

struct Student
{
	char Name[20];
	char ANumber[9];
	int Age;
	float GPA;
};

void studentPrint(Student [], int size);

int main(){

	Student A[10];
        //Changed C to B, no specific reason
	Student B[10];

	//Populates the empty A array
	for (int i = 0; i < 10; i++){
		if (i >= 0 && i < 5){
			strcpy_s(A[i].Name, 21, "Rob Smith");
			strcpy_s(A[i].ANumber, 10, "A67241879");
			A[i].Age = 12;
			A[i].GPA = 3.49;
		}
		else{
			strcpy_s(A[i].Name, 21, "John Doe");
			strcpy_s(A[i].ANumber, 10, "A01241239");
			A[i].Age = 17;
			A[i].GPA = 2.90;
		}
	}

	fstream file;

        // was not meant to use a loop
	file.open("Record.bin", ios::out | ios::binary);
	file.write((char *) A, sizeof(A));
	file.close();

        //was not meant to use a loop
	file.open("Record.bin", ios::in | ios::binary);
	file.clear(); 	
        file.seekg(0L, ios::beg);
	file.read((char *) B, sizeof(B));
	studentPrint(B, 10);
	file.close();

	system("pause");
	return 0;
}

void studentPrint(Student a[], int size){
	for (int L = 0; L < 10; L++){
		cout << "Student Number " << L+1 << "  *****" << endl;
		cout << "Student Name: " << a[L].Name << endl;
		cout << "Student A Number: " << a[L].ANumber << endl;
		cout << "Student Age: " << a[L].Age << endl;
		cout << "Student GPA: " << a[L].GPA << endl;
		cout << endl;
	}
}
Last edited on
Topic archived. No new replies allowed.