Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* basic operations with files, UNIX-native */
- /* pointer arithmetics:
- *
- * push(pointer, v) INDICATES *(pointer++) = v
- * pop(pointer) INDICATES *(--pointer)
- * (*++v)[0] INDICATES (**++v)
- * INDICATES first char in v
- * INDICATES name of string/vector v
- * likewise, *v[0] INDICATES **v
- * and *v[n] INDICATES **(v + n)
- * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
- * char **argv INDICATES pointer to char pointer
- * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
- * int *v[len] INDICATES vector "v" of "len" pointers to int elements
- * void *funct() INDICATES function "funct" that returns a pointer-to-void
- * void (*funct)() INDICATES pointer to a function "funct" that returns void
- *
- */
- /* useful characters: [] # */
- # include <stdio.h>
- # include <fcntl.h> // option_flags are defined here
- // # include <unistd.h> // execlp is defined here; undeclared because it causes error with unlink
- # define BUFFERSIZE 1024
- int main(int argc, char * argv[])
- {
- /* create file */
- if(creat("abc.txt", 0666) == -1) // creates the new file
- printf("File could not be created\n");
- /* get descriptor and close file */
- int descriptor = open("abc.txt", O_RDWR|O_CREAT); // obtains the descriptor of a file
- if(descriptor == -1) printf("File could not be opened\n");
- else close(descriptor); // if the descriptor is valid, the file is closed
- /* read and write */
- // DESCRIPTOR will be used without being re-instantiated
- char * buffer[BUFFERSIZE];
- descriptor = open("testfile.txt", O_RDONLY);
- read(descriptor, buffer, 10); // reads the first 10 CHARS from the file referenced by descriptor
- buffer[10] = '\0'; // escape; indicates end of line
- printf("Read: %s\n", buffer); // writes on standard output stdout
- close(descriptor);
- /* copies the input file into another file */
- // BUFFER will be used without being re-instantiated
- int i; // ID of the source file; if <= 0, it's invalid
- int file = open(*(argv + 1), O_RDONLY); // opens the source file
- int filecopy = open(*(argv + 2), O_WRONLY|O_CREAT|O_TRUNC, 0666); // creates the copy (in a blank state)
- while((i = read(file, buffer, BUFFERSIZE)) > 0) // as long as there are characters to copy...
- write(filecopy, buffer, i); // ...they are copied into the output file
- /* linking */
- /* TEST RESULT:
- link(A, B) creates a "copy" of A, named B
- unlink(A, B) removes A!
- */
- link(*(argv + 1), "secondalias.txt"); // the two files are linked (they are aliases of each other)
- unlink(*(argv + 1), "secondalias.txt"); // then they are unlinked
- /* duplication */
- // DESCRIPTOR will be used without being re-instantiated
- /* TEST RESULT:
- dup(A) duplicates the file ON STDOUT and does NOT create a hdd copy
- */
- descriptor = open("secondalias.txt", O_RDONLY);
- close(0); // closes the standard input
- dup(descriptor);
- execlp("more", "more", 0); // executes system call "more": shows file content on stdout
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement