RosettaCodeData/Task/Truncate-a-file/FutureBasic/truncate-a-file.basic

44 lines
1.1 KiB
Plaintext

include "NSLog.incl"
void local fn ShowFileContents( fileURL as CFURLRef )
CFStringRef string = fn StringWithContentsOfURL( fileURL, NSUTF8StringEncoding, NULL )
NSLog(@"%@",string)
end fn
void local fn TruncateFile( fileURL as CFURLRef, length as UInt64 )
ErrorRef err = NULL
FileHandleRef fh = fn FileHandleForUpdatingURL( fileURL, @err )
if ( !fh )
NSLog(@"%@",err)
return
end if
UInt64 offset
fn FileHandleSeekToEnd( fh, @offset, NULL )
if ( length >= offset )
NSLog(@"Error: Truncate length is not less than file length.")
return
end if
if ( fn FileHandleTruncateAtOffset( fh, length, @err ) )
fn ShowFileContents( fileURL )
else
NSLog(@"%@",err)
end if
end fn
void local fn Doit
// create
CFURLRef desktopURL = fn FileManagerURLForDirectory( NSDesktopDirectory, NSUserDomainMask )
CFURLRef fileURL = fn URLByAppendingPathComponent( desktopURL, @"MyFile.txt" )
fn StringWriteToURL( @"abcdefghijklmnopqrstuvwxyz", fileURL, YES, NSUTF8StringEncoding, NULL )
fn ShowFileContents( fileURL )
// truncate
fn TruncateFile( fileURL, 13 )
end fn
fn DoIt
HandleEvents