Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Truncate file at a position

Status
Not open for further replies.

anasso

Programmer
Jul 18, 2009
3
DK
Hi

I'm writing what should be a portable Fortran library in Fortran 77.
One of the routines should shorten a (direct access) file. For example, if a file is 100 bytes long, you should be able to throw away the last 10 bytes.

In gfortran, I can open a file with record length 1, read record N and use the ENDFILE statement.
This cuts away all but the first N bytes as desired. However, the Fortran 77 standard prohibits using ENDFILE on direct access files.

Is there a 'standard' trick for shortening files like this, or will I have to resort to another language for this particular functionallity?

Anders
 
Hi again

Having read through the documentation more thoroughly, it does indeed seem there is no standard way to make sure a direct access file is exactly N bytes long.

I'll go with a short C function and some preprocessing directives for posix/win32/others.
 
Alas, no portable way to truncate file in the C Standard (except to truncate to zero length;)...
 
The only way I know of doing this is to copy one file to another and stop where you want to truncate. Then delete the old file and rename the new file to the old file. That will work everywhere.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top