This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

ftruncate does not work with rw file handles


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");
    return;
  }
  int fd = fileno(f);
  errno=0;
  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**)
{
  test("rw");
  test("w");
  return 0;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]