Shrinking Files in C

Started by
8 comments, last by SynLogek 22 years, 7 months ago
Well I''m not sure if its possible by the looks of it, but is it possible to open an existing file in C and delete a part of its contence? I''m dealing with files that are up to 500mb so the idea of recreating the file with just the stuff i want is a bit of a slow process. If its even possible to remove just the data at the end of the file to make the file smaller is fine but I can''t even do that. Any help would help! Thank you, Phil.
Advertisement
You could write a program that read the file and writes only the wanted contents to another file, before deleting the original file. Should be pretty easy to implement.

==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Read the file? And how do you expect to do that? A 500Mb file will not fit into most peoples memory, so "reading" it is a bit out of the question. I understand what you mean though SynLogek, all though I don''t know how to do it You trying to impliment a type of archive?
If at first you don't succeed, redefine success.
Ok, I''ve just searched a bit on this ( I could do with knowing a bit about it aswell ), and I have found a few functions that could do it. Firstly, there is WriteFileEx(), which can write data anywhere in a file, and SetEndOfFile() which can increase or decrease the size of a file. Look them and related functions/structures up in the msdn.

Hope that helps
If at first you don't succeed, redefine success.
Thanks for the help. Those functions will help a lot. But is there any low-level functions that can be used only in dos that might be platform independent? That is my ultimate goal.
Why only C/dos? I suggest you grab Win95 and a Windows C/C++ compiler, dev-cpp is a great free one! Then if you follow the windows documentation, you can port it to virtually every Windows platform. Windows will own more than 50% of the OS market for the next 10 years or so...why not...go with winblows!
---------------------------------------------------------------------------------------"Advances are made by answering questions. Discoveries are made by questioning answers." - Bernhard HaischReactOS
Well I still prefer to make it cross-platform compatable. All the code so far is. Its just shrinking the file. I''m using Visual C++ 6 and using those functions mentioned early work awsome, its just you have to be on a windows platform.

I''m just being a pain.

But if anyone knows which functions will work in dos to shrink a file, I still can''t do it!
Oh ya, I found it.

int chsize( int handle, long size );

or

int _chsize( int handle, long size );

Shows what a bit of persistance does. Had to find it in the header directly. But then realized its also in the msdn library.

Thank you all for your help!
Why can''t you just read part of the file (say 1 Meg) at a time, remove whatever data you don''t want, then write it to another file? Reading it in chuncks should reduce the memory requirements...
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Well, I didn''t want to have to rename the file everytime. And transfering that much data is a pain and very slow. The file is a archive of other files, so rearranging the data isn''t so hard using the base level functions.

This topic is closed to new replies.

Advertisement