• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 426
  • Last Modified:

file and fseek

I have an opening binary file with pre-defined max file size (i.e. 10MB). When EOF or >10MB, I use fseek to seek back to beginning of the file and continue on writing. The problem I'm having now is whenever I close the file (after fseek) and the file pointer is somewhere in the middle of the file, the file size is still 10MB. What I like to know is suppose the file pointer is at 6MB, and I close the file, it should close at that point, the rest of 4MB must be discarded (which currently isn't).
1 Solution
You can use the truncate(2) function call to truncate the file to a specified size.

Yes, truncate or ftruncate are the functions you would need to use.

The reason you need to do this is that UNIX filesystems support the concept of 'holes'.  That is, if you needed a random access binary file (say, a big honking array) you may seek forward to a place way past anyplace you've ever written to.  Rather than filling the skipped segments with cruft, it just pretends it wrote it.

Say you open a file for writing, write a few bytes, and then seek out to, say 100MB, and write a few more bytes and close the file.  What actually happens is you end up with a sparse file which may actually occupy only a few bytes of disk.  

The output of the 'ls' command will show the file as actually occupying 100MB, even though du says otherwise.  In this way you can have a sparsely populated file which appears to be that big but doesn't actually use all of the disk space.

When you seek back to the start, the filesystem does not know that you didn't mean to create a file with a hole in it.

Believe it or not, there are cases where this is exactly what you need to do.

beneflexAuthor Commented:
Thanks guys! I forgot to mention that I'm running windows. Is there anyway to do in ANSI C standard? Windows has _chsize() but I'm trying to avoid it because my program will eventually be used in Linux as well. So, it should work well in both windows and linux.
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

#ifdef _WINDOWS
#define truncate _chsize

this assumes they take the same parameters, if not then make your own function call that encapsulates the truncate and chsize, for example

void my_change_size(int fhandle,int new_size, int option_that_win_donot_need)
#ifdef _WINDOWS

then you can use this on any platform and it will compile down correctly, if on windows the extra (i have just invented this paramater to illustrate the point) will no be used

Hope this provides some help
beneflexAuthor Commented:
Thank you, makerp!! One more thing, besides _chsize(), is there any other way to close the file ptr (somewhere in the middle of the file as in my case) and save whatever at that position, the rest will be discarded?
i do not think so.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now