-1

SYNOPSIS

    #include <unistd.h>
    #include <sys/types.h>
    int ftruncate(int fd, off_t length); 

Someone says that offset types are usually long integers. So I use %ld to scanf it as follows:

    off_t size;
    scanf("%ld",&size);
    ftruncate(fout,size);

But the compiler warn :expected "int" but argumant is of type "struct FILE *" What can I deal with it?

wind910037
  • 21
  • 5

2 Answers2

2

The ftruncate function expects its first argument to be a file descriptor, while you pass struct FILE* instead, apparently. The correct way is:

ftruncate(fileno(fout),size);
Oleg Andriyanov
  • 5,069
  • 1
  • 22
  • 36
  • I was about to write this - but here is the link to the relevant manual page http://linux.die.net/man/3/fileno – Ed Heal Jan 09 '16 at 08:48
2

OP has 2 issues. The first, wrong use of ftruncate() is well answered by @Oleg Andriyanov

The 2nd issue if how to use scanf() with off_t. off_t is a signed type: ref.

If off_t was always long, then there would be no reason to define a new type. So assuming off_t is long is a not portable.

To read an integer that is signed with unknown width, suggest reading it using the maximum width.

intmax_t wide_int;
scanf("%jd", &wide_int);
off_t size = (off_t) wide_int;

The above omits error checking. Robust error checking code would use fgets(), strtoimax(), etc.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256