Skip to content
Snippets Groups Projects
Commit ec40022c authored by Marc Feger's avatar Marc Feger
Browse files

Add File Handling Exampler.

parent 479171ce
No related branches found
No related tags found
No related merge requests found
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment