Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7.  
  8. int main(int argc, char const *argv[])
  9. {
  10.     if (argc != 3 && argc != 4) {
  11.         fprintf(stderr, "%s\n", "wrong arguments");
  12.         return 1;
  13.     }
  14.     if (argc == 3) {
  15.         fprintf(stderr, "%s\n", "with append");
  16.         int fd = open(argv[1], O_WRONLY | O_CREAT | O_APPEND);
  17.         if (fd == -1) {
  18.             fprintf(stderr, "%s\n", "error while opening");
  19.             return 1;
  20.         }
  21.         int num_bytes = atoi(argv[2]);
  22.         for (int i = 0; i < num_bytes; ++i) {
  23.             write(fd, "0", 1);
  24.         }
  25.         close(fd);
  26.     } else {
  27.         fprintf(stderr, "%s\n", "without append");
  28.         int fd = open(argv[1], O_WRONLY | O_CREAT);
  29.         int num_bytes = atoi(argv[2]);
  30.         if (fd == -1) {
  31.             fprintf(stderr, "%s\n", "error while opening");
  32.             return 1;
  33.         }
  34.         for (int i = 0; i < num_bytes; ++i) {
  35.             lseek(fd, 0, SEEK_END);
  36.             write(fd, "0", 1);
  37.         }
  38.         close(fd);
  39.     }
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement