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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
| #include <iostream>
#include <stdlib.h>
#include <cstring>
#include <stdio.h>
#include <locale.h>
#include <windows.h>
#include <sys/stat.h>
struct mystruct {
float edge;
char color[20];
char material[20];
};
int main()
{
int st = 0;
printf("Enter number corresponding to the necessary functions:\n1.Overwriting a file\n2.Adding a new data file\n3.Calculation of the total amount of the selected color cubes\n");
scanf_s("%d", &st);
if (st != 1 && st != 2 && st != 3)
{
return 0;
}
if (st == 1)
{
int amount = 0;
int b = sizeof(mystruct);
printf("Enter amount cubes:");
scanf_s("%d", &amount);
FILE *pFile;
pFile = fopen("file.dat", "wb");
mystruct* block = new mystruct[amount];
for (int i = 0; i<amount; i++)
{
printf("\nEnter the size of the edges %d block: ", i + 1);
scanf_s("%f", &block[i].edge);
printf("\nSpecify the color %d block: ", i + 1);
getchar();
gets_s(block[i].color);
printf("\nspecify material %d block: ", i + 1);
gets_s(block[i].material);
}
fwrite(&block, b, amount, pFile);
fclose(pFile);
}
if (st == 2)
{
int newamount = 0;
int b = sizeof(mystruct);
printf("Enter amount new cubes:");
scanf_s("%d", &newamount);
FILE *pFile;
pFile = fopen("file.dat", "ab");
mystruct* block = new mystruct[newamount];
for (int i = 0; i<newamount; i++)
{
printf("\nEnter the size of the edges %d block: ", i + 1);
scanf_s("%f", &block[i].edge);
printf("\nSpecify the color %d block: ", i + 1);
getchar();
gets_s(block[i].color);
printf("\nspecify material %d block: ", i + 1);
gets_s(block[i].material);
}
fwrite(&block, b, newamount, pFile);
fclose(pFile);
}
struct stat fi;
stat("file.dat", &fi);
int b = sizeof(mystruct);
int amount = fi.st_size / b;
mystruct* cub = new mystruct[amount];
FILE *rFile;
rFile = fopen("file.dat", "rb");
if (rFile == NULL)
{
printf("Erorr");
return 0;
}
fread(&cub, b, amount, rFile);
for(int i=0;i<amount;i++)
{
printf("%f %s %s\n",cub[i].edge, cub[i].color, cub[i].material);
}
fclose(rFile);
system("pause");
return 0; |