diff --git a/Open_Close_Read_Write/file_handling.c b/Open_Close_Read_Write/file_handling.c new file mode 100644 index 0000000000000000000000000000000000000000..00056d117be4ebf5a49e4e0bbeb947d712c6c3f9 --- /dev/null +++ b/Open_Close_Read_Write/file_handling.c @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <string.h> + +int main(void){ + + int FLAG = O_RDONLY; + int file = open("/proc/cpuinfo", FLAG); + + int size = 1000; + char *buffer = malloc(size * sizeof(char)); + if(read(file, buffer, size) == -1){ + perror("read error"); + free(buffer); + return -1; + } + + char var_name[size]; + char var_value[size]; + sscanf(buffer, "%*s %*s %*s %s %*s %s", var_name, var_value); + + printf("%s: %s\n", var_name, var_value); + + int FLAG2 = O_WRONLY; + int FLAG3 = O_CREAT; + int FLAG4 = O_APPEND; + int file2 = open("output.txt", FLAG2 | FLAG3 | FLAG4); + + char *msg = "Hallo Welt\n"; + + + if(write(file2, msg, strlen(msg)) == -1){ + perror("write error"); + return -1; + } + + + free(buffer); + close(file); + close(file2); + return 0; +}