summaryrefslogtreecommitdiffstats
path: root/c_pgms/truncate/fd_truncate.c
diff options
context:
space:
mode:
Diffstat (limited to 'c_pgms/truncate/fd_truncate.c')
-rw-r--r--c_pgms/truncate/fd_truncate.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/c_pgms/truncate/fd_truncate.c b/c_pgms/truncate/fd_truncate.c
new file mode 100644
index 0000000..1425ed0
--- /dev/null
+++ b/c_pgms/truncate/fd_truncate.c
@@ -0,0 +1,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);
+}