include stdio include conio include stdlib include time define ERROR_O

 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
63
64
65
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include<time.h>
#define ERROR_OPEN_FILE -3
void main() {
FILE *iofile = NULL;
FILE *iofile1 = NULL;
unsigned counter = 0;
int num,num1;
int yn;
num=rand()%10;
iofile = fopen("numbers.bin", "w+b");
if (iofile == NULL) {
printf("Error opening file");
getch();
exit(ERROR_OPEN_FILE);
}
iofile1= fopen("by4.bin", "w+b");
if (iofile == NULL) {
printf("Error opening file");
getch();
exit(ERROR_OPEN_FILE);
}
fwrite(&counter, sizeof(int), 1, iofile);
do {
printf("enter new number? [1 - yes, 2 - no]");
scanf("%d", &yn);
if (yn == 1) {
//scanf("%d", &num);
num=rand()%10;
fwrite(&num, sizeof(int), 1, iofile);
counter++;
} else {
rewind(iofile);
//fwrite(&counter, sizeof(int), 1, iofile);
break;
}
} while(1);
int i,j;
for (i = 0; i < counter; i++) {
fread(&num, sizeof(int), 1, iofile);
//printf("%d\n", num);
if (i%4==0){
printf("%d\n", num);
fwrite(&num, sizeof(int), 1, iofile1);
}
}
for (i = 0; i < counter; i++) {
fread(&num1, sizeof(int), 1, iofile1);
printf("%d\n", num1);
}
fclose(iofile);
fclose(iofile1);
getch();
}