summaryrefslogtreecommitdiffstats
path: root/c_pgms/truncate/fd_truncate.c
blob: 1425ed0e8f3164d76f9f2fed7ec11931444db804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>

int main ()
{
        int     ret = -1;
        int     fd  = -1;
        char   *str = "This is a string";
        char    pwd[255] = {0, };

        fd = open ("test_file", O_RDWR);
        if (fd == -1) {
                       fprintf (stderr, "ERROR: open failed on the file test_file (%s)",
                        strerror (errno));
        }
        ret = ftruncate (fd, 10);
        if (ret == -1) {
                fprintf (stderr, "ERROR: truncate failed on the file %s (%s)",
                         pwd, strerror (errno));
        }

        if (fd > 0)
                close (fd);
}