-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomFile.c
More file actions
62 lines (54 loc) · 1.29 KB
/
randomFile.c
File metadata and controls
62 lines (54 loc) · 1.29 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
int randNumGen(){
int fd = open("/dev/random", O_RDONLY);
int buf[1];
int bytes = read(fd, buf, sizeof(int));
return buf[0];
close(fd);
}
int main(){
//Create int array...
int arr[10];
int i;
for(i = 0; i < 10; i++){
arr[i] = randNumGen();
}
//Print array values...
printf("Original data read...\n");
for(i = 0; i < 10; i++){
printf("Index %d:\t%d\n", i, arr[i]);
}
//Write to another file...
int wrtr = open("file.txt", O_RDWR | O_CREAT, 0644);
if (wrtr == -1) printf("Error #%d: %s", errno, strerror(errno));
write(wrtr, arr, sizeof(int) * 10);
printf("\nData written to file...\n");
close(wrtr);
//Reading into new array...
int veri[10];
int rdr = open("file.txt", O_RDONLY);
read(rdr, veri, sizeof(int) * 10);
if (rdr == -1) printf("Error #%d: %s", errno, strerror(errno));
close(rdr);
//Print new array...
printf("\nVerification...\n");
for(i = 0; i < 10; i++){
printf("Index %d:\t%d\n", i, veri[i]);
}
//Checking arrays...
printf("\nPrinting final check...\n");
for(i = 0; i < 10; i++){
if(arr[i] == veri[i]){
printf("Index %d equal?\tTRUE\n", i);
}
else{
printf("Index %d equal?\tFALSE\n", i);
}
}
printf("\nTesting concluded...\n");
}