Advertisement
heavenriver

files.c

Nov 25th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1.  /* basic operations with files, UNIX-native */
  2.  
  3.  /* pointer arithmetics:
  4.   *
  5.   * push(pointer, v) INDICATES *(pointer++) = v
  6.   * pop(pointer) INDICATES *(--pointer)
  7.   * (*++v)[0] INDICATES (**++v)
  8.   *           INDICATES first char in v
  9.   *           INDICATES name of string/vector v
  10.   * likewise, *v[0] INDICATES **v
  11.   *       and *v[n] INDICATES **(v + n)
  12.   * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
  13.   * char **argv INDICATES pointer to char pointer
  14.   * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
  15.   * int *v[len] INDICATES vector "v" of "len" pointers to int elements
  16.   * void *funct() INDICATES function "funct" that returns a pointer-to-void
  17.   * void (*funct)() INDICATES pointer to a function "funct" that returns void
  18.   *
  19.   */
  20.  
  21.  /* useful characters: [] # */
  22.  
  23.  # include <stdio.h>
  24.  # include <fcntl.h> // option_flags are defined here
  25.  // # include <unistd.h> // execlp is defined here; undeclared because it causes error with unlink
  26.  
  27.  # define BUFFERSIZE 1024
  28.  
  29.  int main(int argc, char * argv[])
  30.     {
  31.      /* create file */
  32.      if(creat("abc.txt", 0666) == -1) // creates the new file
  33.         printf("File could not be created\n");
  34.    
  35.    
  36.     /* get descriptor and close file */
  37.     int descriptor = open("abc.txt", O_RDWR|O_CREAT); // obtains the descriptor of a file
  38.     if(descriptor == -1) printf("File could not be opened\n");
  39.     else close(descriptor); // if the descriptor is valid, the file is closed
  40.    
  41.    
  42.     /* read and write */
  43.     // DESCRIPTOR will be used without being re-instantiated
  44.     char * buffer[BUFFERSIZE];
  45.     descriptor = open("testfile.txt", O_RDONLY);
  46.     read(descriptor, buffer, 10); // reads the first 10 CHARS from the file referenced by descriptor
  47.     buffer[10] = '\0'; // escape; indicates end of line
  48.     printf("Read: %s\n", buffer); // writes on standard output stdout
  49.     close(descriptor);
  50.    
  51.    
  52.     /* copies the input file into another file */
  53.     // BUFFER will be used without being re-instantiated
  54.     int i; // ID of the source file; if <= 0, it's invalid
  55.     int file = open(*(argv + 1), O_RDONLY); // opens the source file
  56.     int filecopy = open(*(argv + 2), O_WRONLY|O_CREAT|O_TRUNC, 0666); // creates the copy (in a blank state)
  57.     while((i = read(file, buffer, BUFFERSIZE)) > 0) // as long as there are characters to copy...
  58.     write(filecopy, buffer, i); // ...they are copied into the output file
  59.    
  60.    
  61.     /* linking */
  62.     /* TEST RESULT:
  63.        link(A, B) creates a "copy" of A, named B
  64.        unlink(A, B) removes A!
  65.      */
  66.     link(*(argv + 1), "secondalias.txt"); // the two files are linked (they are aliases of each other)
  67.     unlink(*(argv + 1), "secondalias.txt"); // then they are unlinked
  68.    
  69.    
  70.     /* duplication */
  71.     // DESCRIPTOR will be used without being re-instantiated
  72.     /* TEST RESULT:
  73.        dup(A) duplicates the file ON STDOUT and does NOT create a hdd copy
  74.      */
  75.     descriptor = open("secondalias.txt", O_RDONLY);
  76.     close(0); // closes the standard input
  77.     dup(descriptor);
  78.     execlp("more", "more", 0); // executes system call "more": shows file content on stdout
  79.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement