Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting the content of a text file

Status
Not open for further replies.

NatMinx

Programmer
Jun 24, 2008
10
0
0
CA
Ok... here's my problem:
I have 2 files File1 & File2 in the current dir.

Code:
File1:        File2:        tempFile:
20            40            20
30                          30
40

My program is supposed to check if File2's content exists in File1 and delete it.

The way I'm approaching it is:
1) I check if File2's content exists in File1.
2) Then I create a tempFile and append to it everything in File1 but 40(which is File2's content).
3) Now I want to over-write File1's content with tempFile's content, but I'll end up with 20 30 40 again...

My question is, is it possible to completely delete File1's content so that I can easily just write tempFile's content into File1? If so, how? If not, what other option do I have?

Thanx in advance! :)
 
Most file I/O is OS specific except for a few standard C functions like fopen(), fread(), fwrite()...
I belive the only way to truncate a file is by using a platform API call.

What OS are you using?

You could also delete File1 and rename the tempfile to File1.
 
You could open the file for writing, write a blank line to it and then close it. Works on most OSs except VAX/VMS.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top