Solved

file and fseek

Posted on 2001-07-19
6
392 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
Comment Utility
You can use the truncate(2) function call to truncate the file to a specified size.

0
 
LVL 1

Expert Comment

by:gstoddart
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Accepted Solution

by:
makerp earned 100 total points
Comment Utility
#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
Comment Utility
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
Comment Utility
i do not think so.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 opening and writing to files 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.

763 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

11 Experts available now in Live!

Get 1:1 Help Now