file and fseek

Posted on 2001-07-19
Last Modified: 2010-08-05
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).
Question by:beneflex

Expert Comment

ID: 6300392
You can use the truncate(2) function call to truncate the file to a specified size.


Expert Comment

ID: 6300659
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.


Author Comment

ID: 6300720
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.
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

LVL 10

Accepted Solution

makerp earned 100 total points
ID: 6301444
#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

Author Comment

ID: 6315668
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?
LVL 10

Expert Comment

ID: 6316407
i do not think so.


Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question