file_demo.c
author Markus Brökers <mbroeker@largo.homelinux.org>
Mon, 09 Aug 2010 12:12:55 +0200
changeset 137 d8a0984b72fc
parent 131 b5ad49852adc
child 138 dff18d1ac2af
permissions -rw-r--r--
From a 'C-Professional' :) Take a look at the source and pray

/**
 * file_demo.c
 * Copyright (C) 2008 Markus Broeker
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>

int main (int argc, char **argv)
{
    int fd1;
    int fd2;
    int mode;

    char *str = "Schreib mal was!";

    if (argc != 2)
        return -1;
    mode = strtoul (argv[1], NULL, 8);

    if (system ("/bin/rm -f /home/mbroeker/Desktop/test1.txt") != 0)
        return EXIT_FAILURE;
    if (system ("/bin/rm -f /home/mbroeker/Desktop/test2.txt") != 0)
        return EXIT_FAILURE;

    /*
     * Opens ReadOnly
     */
    fd1 = open ("/home/mbroeker/Desktop/test1.txt", O_CREAT, mode);
    fd2 = open ("/home/mbroeker/Desktop/test2.txt", O_RDWR | O_CREAT, mode);

    if (fd1)
        if (write (fd1, str, strlen (str)) == -1)
            perror ("FD1");

    if (fd2)
        if (write (fd2, str, strlen (str) + 1) == -1)
            perror ("FD2");

    close (fd1);
    close (fd2);

    return EXIT_SUCCESS;
}