Solved

file and fseek

Posted on 2001-07-19
6
395 Views
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).
0
Comment
Question by:beneflex
6 Comments
 
LVL 3

Expert Comment

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

0
 
LVL 1

Expert Comment

by:gstoddart
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.

Cheers
0
 

Author Comment

by:beneflex
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.
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 10

Accepted Solution

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

Author Comment

by:beneflex
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?
0
 
LVL 10

Expert Comment

by:makerp
ID: 6316407
i do not think so.

0

Featured Post

Why are Office 365 signatures so complicated?

Trying to setup transport rules for Office 365 email signatures and can’t quite figure it out? Having to test the signature over and over? Make things simple by using Exclaimer Cloud - Signatures for Office 365.

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.

932 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now