ftruncate does not work with rw file handles

Marc Pawlowsky
Tue Dec 12 20:08:00 GMT 2017

Is it intentional that ftruncate does not work with read/write file
handles, or am I doing somthing wrong?

gcc t.cpp && ./a.out
mode=rw fd=3 err=-1 errno=22 Invalid argument
mode=w fd=4 err=0 errno=0 Success

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>

static void test(const char* mode)
  FILE* f = fopen("/tmp/tt", mode);
  if (!f) {
    printf("Unable to open  file\n");
  int fd = fileno(f);
  int err = ftruncate(fd,0);
  printf("mode=%s fd=%d err=%d errno=%d %s\n",
    mode, fd, err,
    int(errno), strerror(errno));
int main(int,char**)
  return 0;

More information about the Libc-help mailing list