[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • 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).
0
beneflex
Asked:
beneflex
1 Solution
 
interiotCommented:
You can use the truncate(2) function call to truncate the file to a specified size.

0
 
gstoddartCommented:
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.

Cheers
0
 
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
makerpCommented:
#ifdef _WINDOWS
#define truncate _chsize
#endif

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
 _chsize(fhandle,new_size)
#else
 truncate(fhandle,new_size,option_that_win_donot_need)
#endif
}

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
0
 
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?
0
 
makerpCommented:
i do not think so.

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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