-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfehler.c
47 lines (38 loc) · 905 Bytes
/
fehler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* Module: fehler.c
* Author: mp3skater
* License: MIT License
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int fehl(int count, char* orig_name, char* name)
{
// Open file (reading)
FILE* orig_text;
orig_text = fopen(orig_name, "r");
// Create file (writing)
FILE *text;
text = fopen(name, "w");
// Error
if(orig_text == NULL || text == NULL) {
printf("--> Error while creating/reading a file.");
return 1;
}
// Buffer: 8 chars + parity bit + null terminator
char buf[10];
// Random init
srand(time(NULL));
// Inserts <count> errors
int errs = 0;
while(fgets(buf, sizeof(buf), orig_text) != NULL) {
if(errs < count)
buf[rand()%8] ^= 1; // turn 1 into 0 and 0 into 1 in a random place somehow
fprintf(text, "%s", buf);
errs++;
}
// Very important: Close the files
fclose(orig_text);
fclose(text);
return 0;
}