How do I clear a textfile in a program

Hi guys, I need to clear a textfile after I click a button instead of deleting it. I found a few codes online but I can't seem to get them to work.

I need to clear a file named "train.txt", which I have saved in a folder at "D:\My Documents\Visual Studio 2005\Projects\zebra\zebra"

Below is what I have done.

1
2
3
4
5
void CzebraDlg::OnBnClickedClearimagedatabase()
{
	std::cin >> train.txt;
	std::cin >> "";
}


I'm currently using MFC for the GUI creation. The headers and libraries I have included for this program are so far:

1
2
3
4
5
6
7
#include "stdafx.h"
#include "zebra.h"  //program header created by me
#include "zebraDlg.h" //GUI implementation header
#include <string>
#include <fstream>
#include "windows.h"
#include <iomanip> 


Can anyone help? Thanks a lot in advance!
hello
let's reOpen it,and close it!

ofstream ofile( "train.txt", ios::out );
if (!ofile.is_open())
{
std::cout << "OpenFile train.txt Errors!" << std::endl;
}
ofile.close();

If the file already exists, the old version is deleted.
my English is poor, i hope you can understand.
Hi ,
Why cant you delete the file and create the new file with the same name .
or you can create open the file in the write mode and then close it which will delete all its content .
If you want to ensure deletion of the contents, open the file with the ios::trunc flag... which will truncate the file;)...
Topic archived. No new replies allowed.