file_demo.c
changeset 138 dff18d1ac2af
parent 131 b5ad49852adc
equal deleted inserted replaced
137:d8a0984b72fc 138:dff18d1ac2af
     8 #include <unistd.h>
     8 #include <unistd.h>
     9 #include <sys/types.h>
     9 #include <sys/types.h>
    10 #include <string.h>
    10 #include <string.h>
    11 #include <fcntl.h>
    11 #include <fcntl.h>
    12 
    12 
       
    13 #define TESTFILE1 "/tmp/test1.txt"
       
    14 #define TESTFILE2 "/tmp/test2.txt"
       
    15 
    13 int main (int argc, char **argv)
    16 int main (int argc, char **argv)
    14 {
    17 {
    15     int fd1;
    18     int fd1;
    16     int fd2;
    19     int fd2;
    17     int mode;
    20     int mode;
    18 
    21 
    19     char *str = "Schreib mal was!";
    22     char *str = "Schreib mal was!";
    20 
    23 
    21     if (argc != 2)
    24     if (argc != 2) {
       
    25         printf ("Usage: %s <MODE>\n", argv[0]);
    22         return -1;
    26         return -1;
       
    27     }
    23     mode = strtoul (argv[1], NULL, 8);
    28     mode = strtoul (argv[1], NULL, 8);
    24 
    29 
    25     if (system ("/bin/rm -f /home/mbroeker/Desktop/test1.txt") != 0)
    30     unlink (TESTFILE1);
    26         return EXIT_FAILURE;
    31     unlink (TESTFILE2);
    27     if (system ("/bin/rm -f /home/mbroeker/Desktop/test2.txt") != 0)
       
    28         return EXIT_FAILURE;
       
    29 
    32 
    30     /*
    33     /*
    31      * Opens ReadOnly
    34      * Opens ReadOnly
    32      */
    35      */
    33     fd1 = open ("/home/mbroeker/Desktop/test1.txt", O_CREAT, mode);
    36     fd1 = open (TESTFILE1, O_CREAT, mode);
    34     fd2 = open ("/home/mbroeker/Desktop/test2.txt", O_RDWR | O_CREAT, mode);
    37     fd2 = open (TESTFILE2, O_RDWR | O_CREAT, mode);
    35 
    38 
    36     if (fd1)
    39     if (fd1)
    37         if (write (fd1, str, strlen (str)) == -1)
    40         if (write (fd1, str, strlen (str)) == -1)
    38             perror ("FD1");
    41             perror ("FD1");
    39 
    42