C++ EOF Problem :( ! Need assistance...

Hi everyone;

In the following code i control whether an empty space in the file or not. If there is no empty space between the records of the file then fread reads eof and i ('m supposed to write) new element to the eof!!! But when fread reads eof, then 'file' does nothing about fseek, fread or fwrite... The last 4 lines just to simplify the code but it again doesn't work: same problem, no movement after reading eof! (here the code must print the last element of the file to the end of file again as you see, it is just for try!)

------------------------------------------------------------------
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
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>

fstream file;

class game
{
public:
	char name[30];
	char category[30];
	int year;
	int rating;
};

void main ()
{
	int index = 0;
	game temp;

	file.open("games.txt", ios::in | ios::out);
	file.read((char *)&temp, sizeof(game));

	while (!file.eof())
	{
		if (temp.rating == -1)	// If an empty place found, then break!
		{
			file.seekg(index * sizeof(game), ios::beg);
			break;
		}

		file.read((char *)&temp, sizeof(game));
		index++;
	}

        //file.seekg(0, ios::beg);
        //file.read((char *)&temp, sizeof(game));
	file.write((char *)&temp, sizeof(game));
	file.close();
}

------------------------------------------------------------------
Last edited on
Topic archived. No new replies allowed.