/* Copyright (c) 2001-2004 by SoftIntegration, Inc. All Rights Reserved */
/* a sample program that opens a file then write a string to it. */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define SIZE 80

int main() {
    int fildes, x;
    char buf[SIZE];
    char *name; 
    
    if((name = tmpnam(NULL)) == NULL) {
        printf("temp file name could not be made. \n");
        exit(1);
    }
    strcpy(buf,"This is a test");
    /* open file for reading only */
    if((fildes = open(name,O_RDWR | O_CREAT)) == -1){ 
        printf("cannot open file\n");
        exit(1);
    }
    write(fildes,buf,SIZE);
/* flush the buffer and store the data to disk at once */
    if((fsync(fildes)) != -1) 
        printf("Buffer is flushed and moved to a permanent storage device.\n"); 
    if(close(fildes)) printf("error in closing file\n");
    remove(name);
}
