Reading/Writing binary records

Hello,

I need to write my own file format, for storing vertices and the like. I've been googling for a few days now and looking at quite a few examples( including ones on this site ). But I'm still having trouble, I'm not used to file I/O in C++ as generally I use middleware that reads/writes its own formats.

Anyway, I eventually settled on a simple C method. I kind of figured that if I could get a simple method working, I'd have a fairer idea in my mind and then be able to move on to streaming more confidently. However, it's not giving me the output I expected. Here is the data structure and my read/write functions.

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
struct vert
{
	float x, y, z;
        float nx, ny, nz;
        float tx, ty, tz;
        float bx, by, bz;
        float tu, tv;
};

struct myFile
{
	vert v;
};

void writeOutFile()
{
	FILE* f;
	struct myFile myRecord;

	f = fopen("test.binary","w");

	for(int i = 0; i <= 10000; i++)
	{
		myRecord.v.x = float(i);
		fwrite(&myRecord, sizeof(struct myFile), 1, f);
	}
	fclose(f);
}

void readInFile()
{
	FILE* f;

	struct myFile myRecord;

	f = fopen("test.binary","r");

	for(int i = 0; i <= 10; i++)
	{
		fread(&myRecord, sizeof(struct myFile), 1, f);
		logFile.open("read.log");

		logFile << "vx: " << myRecord.v.x << std::endl;
		
		logFile.close();
		
	}
	fclose(f);
}


The "read.log" file has just one entry in it.

 
vx: 10


I was expecting it to have the first 11 records 0-10.

Obviously I'm doing something fundamentally wrong and I was hoping some of you guys could point in the right direction.

Thanks. :)
Last edited on
Fixed, stupid thing to do. I was opening and closing the log in the for loop.
Topic archived. No new replies allowed.