Shoutbox

C++ Question (Clearing a File) - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: C++ Question (Clearing a File) (/showthread.php?tid=41496)

C++ Question (Clearing a File) by Maniac on 03-29-2005 at 11:36 PM

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 :'(


RE: C++ Question (Clearing a File) by Ash_ on 03-30-2005 at 12:20 AM

*hint*

CreateFile
WriteFile
CloseHandle


*hint* API Calls go look them up ;)


RE: C++ Question (Clearing a File) by Maniac on 03-30-2005 at 12:28 AM

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


RE: C++ Question (Clearing a File) by segosa on 03-30-2005 at 05:31 AM

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