File lab8 cpp Author smacker Created on 11 Май 2009 16 include stdio i

  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
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* File: lab8.cpp
* Author: smacker
*
* Created on 11 Май 2009 г., 0:16
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stadium {
char name[256];
char address[256];
int capacity;
char sports[256];
};
Stadium new_struct(){
Stadium NewStadium;
char instring[256];
printf("Please enter stadium name: ");
gets(instring);
strcpy(NewStadium.name, instring);
printf("Please enter stadium address: ");
gets(instring);
strcpy(NewStadium.address, instring);
printf("Please enter stadium capacity: ");
gets(instring);
NewStadium.capacity = atoi(instring);
printf("Please enter stadium sports: ");
gets(instring);
strcpy(NewStadium.sports, instring);
return NewStadium;
}
void print_struct(Stadium st){
printf("Stadium name: %s\n", st.name);
printf("Stadium address: %s\n", st.address);
printf("Stadium capacit: %d\n", st.capacity);
printf("Stadium sports: %s\n", st.sports);
printf("---\n");
}
int write(Stadium* sts, int size){
FILE *pFile;
size_t result;
if (sizeof(sts)){
pFile = fopen("/home/smacker/data.bin" , "wb");
if (pFile==NULL) { fputs ("File error", stderr); exit (1); }
result = fwrite (sts, sizeof(Stadium), size, pFile );
if (result != size) { fputs ("Writing error", stderr); exit (2); }
fclose (pFile);
}
return 0;
}
Stadium* read(int &size){
FILE *pFile;
long lSize;
Stadium *buffer;
size_t result;
pFile = fopen("/home/smacker/data.bin" , "rb");
if (pFile==NULL) { fputs ("File error", stderr); exit (1); }
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
buffer = (Stadium*) malloc (lSize);
if (buffer == NULL) { fputs ("Memory error", stderr); exit (2); }
result = fread (buffer, sizeof(Stadium), lSize/sizeof(Stadium), pFile);
if (result != lSize/sizeof(Stadium)) { fputs ("Reading error", stderr); exit (3); }
fclose (pFile);
size = lSize/sizeof(Stadium);
return buffer;
}
int find(Stadium *stadiums, int size, char *name){
for(int i = 0; i < size; i++){
if(strcmp(stadiums[i].name, name) == 0)
{
return i;
}
}
return -1;
}
Stadium* task(Stadium *stadiums, int &size){
char instring[256];
int el;
int j = 0;
printf("Please enter name: ");
gets(instring);
printf("\n");
el = find(stadiums, size, instring);
printf("el:%d", el);
if (el>-1){
size += 1;
Stadium *buffer = stadiums;
stadiums = (Stadium*) malloc (sizeof(Stadium)*(size));
for(int i = 0; i < size+1; i++){
if (i == el){
stadiums[i] = new_struct();
i += 1;
stadiums[i] = new_struct();
}
else {
stadiums[i] = buffer[j];
}
j += 1;
}
}
else {
printf("Element not found\n");
}
return stadiums;
}
int main(int argc, char** argv) {
Stadium *stadiums;
Stadium *buffer;
Stadium st;
int size = 0;
char instring[256];
int running = 1;
while (running){
printf("1 - Add new Stadium\n2 - Read from file\n3 - Make task\n4 - Print\n5 - Save\n6 - Exit\nChoose: ");
gets(instring);
switch (atoi(instring))
{
case 1:
size += 1;
st = new_struct();
buffer = stadiums;
stadiums = (Stadium*) malloc (sizeof(Stadium)*size);
for(int i = 0; i < size-1; i++){
stadiums[i] = buffer[i];
}
stadiums[size-1] = st;
break;
case 2:
stadiums = read(size);
break;
case 3:
stadiums = task(stadiums, size);
break;
case 4:
for(int i = 0; i < size; i++){
print_struct(stadiums[i]);
}
break;
case 5:
write(stadiums, size);
break;
default:
running = 0;
}
printf("\n");
}
return (EXIT_SUCCESS);
}