Help with Binary File Handling

I was making a game (sortof). What I am trying to do is have it keep a Highscore List. It was not working. Before I get to the problem, here is the code:
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
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

class Data
{
	char x[64];
	int y;

public:
	void setx(char X[64]) { strcpy_s(x,X);}
	void sety(int X) { y = X;}

	string getx() {return x;}
	int gety ()   {return y;}

	bool operator < (Data& dat)
	{
		if (this->y < dat.gety())
			return true;
		else
			return false;		
	}
};

void Sort(Data dat[5])
{
	for (int i = 0; i < 5;i++)
	{
			for (int j = i; j<5;j++)
			{
				if (dat[i] < dat[j])
				{
					Data x = dat[i];
					dat[i] = dat[j];
					dat[j] = x;
				}
			}
	}
}

int main ()
{
	ofstream Wri1, Wri2;
	char t[64],ch;
	int y;
	cout << "Enter Data to be input: ";
	cin >> t;

	cin.get(ch);

	cout << "Enter Number; ";
	cin >> y;

	Data te;

	te.setx(t);
	te.sety(y);

	

	Wri1.write((char*)&te, sizeof(te));
		
	Wri1.close();
	ifstream rea;
	rea.open("File.bin");
	Data dat[5];

	int loc = 0;
	for (int i = 0;i<5;i++)
	{
	        rea.seekg(i*sizeof(dat[i]);
		rea.read((char*)&(dat[i]), sizeof(dat[i]));
	}

	Wri2.open("File.bin", ios::out|ios::binary|ios::trunc);

	Sort(dat);

	for (int i = 0; i <5;i++)
	{
		Wri2.seekp(i*sizeof(Data));
		Wri2.write((char*)&(dat[i]), sizeof(Data));
	}

	cin.get();
	return 0;
}


This is a separate program to test out what I was trying to do.
I take the data, and put it in the file, at the end. I will then take in all the input again,sort it ,and replace it in the file, now arranged.

But what happens when I run it is, it takes only the first input I gave it (when the file was created), and nothing after it.

So dat[0] is defined, but the rest are filled with garbage values.

Any help?
Last edited on
Someone?
Please don't take this the wrong way, but shit like this makes my head hurt. If you're using C++, don't use C file and string functions. If you're using C, don't use C++ stdin/stdout functions.

That being said, you haven't defined wri1 or wri2, at least not that I'm aware of. You should be using cstring, not string.h. Please use more descriptive variable names instead of ones that are only 3/4 letters/numbers long. We're in the era of IDE's that have some type of code completion.

Have you attempted to display the information read from the file? Your sort function is also using a form of brute force, and although it would work for all cases, it's slow and not very effective.

I personally can't make heads or tails of the C file functions, so you're on your own with that. Why not just use the C++ file objects? You've already included it into your file and it's a million times easier to use.
I would like to use modern C++ functions, but I am stuck with using Turbo C++ in school, and this is what I must use for the project. I had made some changes in it when I copied it to the Visual C++ compiler, which is why you see the mismatch.

I cannot imagine what happened when I was copying, but I know that I had declared them just after opening the main function. I have edited it with the corresponding code.

Instead of displaying, I was using the debugger to look at the values stored in data. I will output it to the console, and then post it here.

The sort function, I wasn't even trying to be efficient, since I intend to sort at most 11 objects, the speed won't have observable difference. But yes, I am aware that my approach with it is very inefficient.

What do you mean by C++ file objects. From what I understand, file handling is not very different in C and C++.
You're using C++ file objects, but in a C manner. You can just C++ file objects just like you do the I/O stream objects. They also have overloaded >> and << operators. For example, these lines can be rewritten as such:
1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 0;i<5;i++)
	{
	        rea.seekg(i*sizeof(dat[i]);
		rea.read((char*)&(dat[i]), sizeof(dat[i]));
	}
// ...

	for (int i = 0; i <5;i++)
	{
		Wri2.seekp(i*sizeof(Data));
		Wri2.write((char*)&(dat[i]), sizeof(Data));
	}


1
2
3
4
5
6
7
for (int i = 0; i < 5; i ++)
   rea >> dat[i].x >> dat[i].y;

// ...

for (int i = 0; i < 5; i ++)
   Wri2 << dat[i].x << dat[i].y;


The only thing that you need to make sure of is that the pointer is at the top of the file before reading/writing, which is standard. That can change, however, if you read from a file, then delete it or something.

I'm tired, I think I'm going to go to bed now, but yes, please try to use the overloaded >> and << operators from now on. It makes things so much easier to understand.

Oops, I just realized, you haven't overloaded those operators for your class yet. No matter, I fixed the example anyways. Have a good night and I hope you can figure it out.
Last edited on
Ah! I thought they worked for text files only. Even the documentation on this site referred to these functions. Well, I will try to make this code more readable and try to solve it.
Okay. Did it afresh. I didn't find what was causing the problem, but it somehow got fixed. But your suggestion helped quite a bit. Thank You.
Glad it worked. From what I understand, a binary file isn't 101101110 style text, it's simply no special formatting. I can't explain it, but it does behave very similar to a traditional text file. Also, the extension doesn't change what's physically inside of the file, just simply changes the default programs used to open and view it. What you must have read was something along the lines of reading from file from a specific location, that's what the seek functions do, they move the "cursor" in the file to where you want it. However, unless you're comfortable with it or know exactly what is in the file and always know where the cursor is, it can play tricks with you.

Note: I really need to go to bed. -.-
Topic archived. No new replies allowed.