mkostemps.c 767 B

12345678910111213141516171819202122232425262728
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "test_helpers.h"
  5. int main(void) {
  6. char* file_name = (char*) calloc(18, sizeof(char));
  7. strcpy(file_name, "tempXXXXXX.suffix");
  8. int fd = mkostemps(file_name, 7, 0);
  9. FILE* fp = fdopen(fd, "w+");
  10. printf("Start unchanged: %d\n", strncmp(file_name, "temp", 4));
  11. printf("End unchanged: %d\n", strcmp(file_name + 4 + 6, ".suffix"));
  12. char* write = "Writing to file";
  13. fputs(write, fp);
  14. char buffer[sizeof write];
  15. memset(buffer, 0, sizeof buffer);
  16. fgets(buffer, strlen(buffer), fp);
  17. if (strcmp(write, buffer)) {
  18. printf("Read & Write Successful\n");
  19. } else {
  20. printf("Read & Write Failed\n");
  21. }
  22. fclose(fp);
  23. remove(file_name);
  24. }