What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » C++ Question (Clearing a File)

C++ Question (Clearing a File)
Author: Message:
Maniac
Senior Member
****

Avatar
Posts including T&T: 1684

Posts: 549
Reputation: 9
37 / Male / –
Joined: Apr 2004
O.P. Huh?  C++ Question (Clearing a File)
Alright, i'm in desperate help for a very simple problem...

I just finished a program that uses a file to keep track of records. One of the functions in my program loads each record from the file into a linked list and does all sorts of things to them. Long story short, once the function runs, i have a smaller version of the file in the linked list.

Now what i want to do is simple, take the stuff from the linked list and put them in the file... BUT how do i do that? :p

I thought of just going to the beginning of the file then sticking everything in, but considering this "new version" is shorter some of the previous stuff will remain at the end.

I then thought of going over the file and replacing everything with a blank space then added the new version at the beginning... it works... but i don't like the way i did it and its very unefficient...

So my question is... how can you clear a file? (If it is possible i don't mind deleting the file and creating another one with the same name... anything to get the file empty)

I'd appreciate your help... this damned thing is due tomorrow and i can't think anymore :'(
[Image: maniacsig0pb.jpg]
*It took Jesus 3 days to respawn! Talk about lag!* :rofl:
03-29-2005 11:36 PM
Profile E-Mail PM Web Find Quote Report
Ash_
Senior Member
****

Avatar

Posts: 638
Reputation: 31
35 / Male / –
Joined: Aug 2004
RE: C++ Question (Clearing a File)
*hint*

CreateFile
WriteFile
CloseHandle


*hint* API Calls go look them up ;)
[Image: jeansiger5.jpg]
03-30-2005 12:20 AM
Profile PM Find Quote Report
Maniac
Senior Member
****

Avatar
Posts including T&T: 1684

Posts: 549
Reputation: 9
37 / Male / –
Joined: Apr 2004
O.P. RE: C++ Question (Clearing a File)
Ummm that looks like a little more hardcore then i can handle :p I need something very basic, right now it works but the end of the file is filled with empty lines :(

edit: bah nevermind i just left it like that... transfer this into T&T and spam all u want :p

This post was edited on 03-30-2005 at 04:00 AM by Maniac.
[Image: maniacsig0pb.jpg]
*It took Jesus 3 days to respawn! Talk about lag!* :rofl:
03-30-2005 12:28 AM
Profile E-Mail PM Web Find Quote Report
segosa
Community's Choice
*****


Posts: 1407
Reputation: 92
Joined: Feb 2003
RE: C++ Question (Clearing a File)
It's simple. Do what Ash_ said and look up CreateFile().

http://msdn.microsoft.com/library/default.asp?url...ase/createfile.asp

I know it's long, but if you want to be a programmer you have to be capable of reading large amounts of text.

Notice that dwCreationDisposition says this:

code:
[in] Action to take on files that exist, and which action to take when files do not exist. For more information about this parameter, see the Remarks section. This parameter must be one of the following values.

CREATE_ALWAYS

Creates a new file. If the file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor specified by the SECURITY_ATTRIBUTES structure.


So that's all you need to do, use CreateFile(), then CloseHandle().

And since CreateFile is like this,
code:
HANDLE CreateFile(
  LPCTSTR lpFileName,
  DWORD dwDesiredAccess,
  DWORD dwShareMode,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  DWORD dwCreationDisposition,
  DWORD dwFlagsAndAttributes,
  HANDLE hTemplateFile
);


going through each parameter on MSDN you'd end up with

HANDLE hFile=CreateFile("filename.txt", GENERIC_READ, NULL, NULL, CREATE_ALWAYS, NULL, NULL);
CloseHandle(hFile);

That's it. And if you can do linked lists, I'm sure you could have done this. :P

This post was edited on 03-30-2005 at 06:18 AM by segosa.
The previous sentence is false. The following sentence is true.
03-30-2005 05:31 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On