exercise_5_1.c 903 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * Modify the program in Listing 5-3 to use standard file I/O system calls (open() and
  3. * lseek()) and the off_t data type. Compile the program with the _FILE_OFFSET_BITS
  4. * macro set to 64, and test it to show that a large file can be successfully created.
  5. */
  6. // for large file compile with: gcc -D_FILE_OFFSET_BITS=64
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. int
  13. main (int argc, char *argv[])
  14. {
  15. int fd;
  16. off_t off;
  17. if (argc != 3 || strcmp (argv[1], "--help") == 0)
  18. {
  19. printf ("%s pathname offset\n", argv[0]);
  20. return EXIT_FAILURE;
  21. }
  22. fd = open (argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  23. if (fd == -1)
  24. return EXIT_FAILURE;
  25. off = atoll (argv[2]);
  26. if (lseek (fd, off, SEEK_SET) == -1)
  27. return EXIT_FAILURE;
  28. if (write (fd, "test", 4) == -1)
  29. return EXIT_FAILURE;
  30. return EXIT_SUCCESS;
  31. }