overwriting a text file

ive written a program, that keeps overwriting each line in a text document, until the end of the document. the only problem is, the act of overwriting causes it to not be the end of the document anymore. here is the code, how could i fix? or any appropriate tutorials you could point me to?

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

using namespace std;

int main()
{
	int num;
	fstream myfile;
	myfile.open("z.txt");
	if(myfile.is_open())
	{
		num=0;
		while(!myfile.eof())
		{
			++num;
			myfile << "This is line "<<num<<endl;
			cout << num << "\n";
		}
		myfile.close();
	}
	else cout << "Unable to open file";
	return 0;
}
Topic archived. No new replies allowed.