Set a position in file stream to be EOF?

D

dacium

Can you set the current position in a stream to be the new end of
file?

I am opening a file with fopen(filename,"r+b")

I am moving back and forth with fseek to read and write in the file,
but sometimes there is data at the end of the file that I don't want
to be part of the file anymore. How do I truncate it?

All I could find was ftrunctate but its unistd, so I am guessing there
is no standard way accomplish a truncation?

Thanks
 
D

dj3vande

Can you set the current position in a stream to be the new end of
file?

I am opening a file with fopen(filename,"r+b")

I am moving back and forth with fseek to read and write in the file,
but sometimes there is data at the end of the file that I don't want
to be part of the file anymore. How do I truncate it?

All I could find was ftrunctate but its unistd, so I am guessing there
is no standard way accomplish a truncation?

A grep through n869 indicates that you are correct.
(7.19.3#2 says "Binary files are not truncated, except as defined in
7.19.5.3."; 7.19.5.3#3 lists the valid mode arguments for fopen, some
of which cause truncation to zero-length, but I don't see any way to
specify truncation at an arbitrary point in the file.)

Writing a new copy of the file and then using remove() and rename() may
be a suitable portable(-ish) way to accomplish this, but the behavior
of remove() and rename() is specified weakly enough (and varies enough
among popular platforms) that you're not losing a whole lot by just
going straight to using POSIX system calls.

If you have control over the file format, you may be able to avoid
having to truncate by specifying some kind of padding that you can
overwrite any leftover data with.


dave

--
Dave Vandervies dj3vande at eskimo dot com
What's behind all this powerful computer technology?
The UPS (basically a car battery with an IBM badge).
--darwinist and Richard Heathfield in comp.programming
 
E

Eric Sosman

Can you set the current position in a stream to be the new end of
file?

I am opening a file with fopen(filename,"r+b")

I am moving back and forth with fseek to read and write in the file,
but sometimes there is data at the end of the file that I don't want
to be part of the file anymore. How do I truncate it?

All I could find was ftrunctate but its unistd, so I am guessing there
is no standard way accomplish a truncation?

The Standard C library has no way to truncate a file; see
Question 19.13 in the comp.lang.c Frequently Asked Questions
(FAQ) list at <http://www.c-faq.com/>. The FAQ's answer points
out ftruncate(), chsize(), and fcntl() as possible "beyond C"
alternatives.

Another possibility (FAQ Question 19.14) is to leave the
file at its full size, but to write a recognizable "nothing to
see here folks, just move along" pattern in the portion you'd
like to "delete."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top