Unix to Windows Porting Dictionary for HPC

RSS

Links

Function List

truncate, ftruncate


Unix

header file: unistd.h

int truncate (const char *path, off_t length)
int ftruncate (int fd, off_t length)

Windows

header file: io.h

int _chsize(int fd, long size);
errno_t _chsize_s(int fd, __int64 size);

Purpose

The truncate() and ftruncate() functions set a file to the specified length.

Discussion

The Windows _chsize() and _chsize_s() functions perform the same as the Unix ftruncate() function. The difference between the two Windows functions centers mostly with the "size" argument.

With Windows you will need to open the file before requesting the truncating because there is no direct equivalent to truncate().

Example of Use in Windows

int fh, result;
unsigned int nbytes = BUFSIZ;

  /* This replicates the use of truncate */
  if (_sopen_s(&f, "data", _O_RDWR) == 0)
  {
     if (( result = _chsize(f, 12000)) == 0)
        printf("Size changed\n" );
     else
        printf("Size not changed\n)");
     _close(f);
  }
  
blog comments powered by Disqus
Valid HTML 4.01 Transitional Valid CSS!