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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
| #include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
using namespace std;
class ffile
{
private:
char stroka[20][255];
int kol_str;
char filnam[13];
public:
ffile(): kol_str(0)
{}
void vvod_nam()
{
cout<<"Введите имя файла"<<endl;
cin>>filnam;
}
void create_file()
{ int i=0;
cout<<"Введите кол-во строк"<<endl;
cin>>kol_str;
FILE *ff=fopen(filnam,"wt");
if (ff!=NULL)
{
while (i<kol_str)
{
cout<<"ввод "<<i+1<<" строки:"<<endl;
gets(stroka[i]);
if (i<kol_str-1)
strcat(stroka[i],"\n");
fputs(stroka[i],ff);
i++;}
fclose(ff);
}
else
cout<<"Ошибка. Файл не открыт.";
}
void append_file()
{
int n;
cout<<"Введите кол-во строк для добавления"<<endl;
cin>>n;
FILE *ff=fopen(filnam,"at");
if (ff!=NULL)
{
for (int i=kol_str;i<kol_str+n;i++)
{
cout<<"ввод "<<i+1<<" строки:"<<endl;
gets(stroka[i]);
if (i<kol_str-1)
strcat(stroka[i],"\n");
fputs(stroka[i],ff);
}
fclose(ff);
kol_str+=n;
}
else
cout<<"Ошибка. Файл не открыт.";
}
void read_file()
{
FILE *ff=fopen(filnam,"rt");
if (ff!=NULL)
{
int i=0;
while (!feof(ff))
{
fgets(stroka[i],255,ff);
i++;
}
fclose(ff);
kol_str=i;
}
else
cout<<"Ошибка. Файл не открыт.";
}
void print_file()
{
read_file();
for (int i=0;i<kol_str;i++)
puts(stroka[i]);
}
void change_file()
{
char new_str[255];
int nom_str;
cout<<"Введите номер стоки, которую хотите изменить"<<endl;
cin>>nom_str;
cout<<"Заменяемая стока:"<<endl;
puts(stroka[nom_str-1]);
cout<<"Введеная строка:"<<endl;
gets(stroka[nom_str-1]);
if (nom_str<kol_str)
strcat(stroka[nom_str-1],"\n");
FILE *ff=fopen(filnam,"wt");
if (ff!=NULL)
{
for (int i=0;i<kol_str;i++)
fputs(stroka[i],ff);
fclose(ff);
}
}
void copy_file(ffile f1)
{
cout<<"Введите имя файла"<<endl;
cin>>filnam;
kol_str=f1.kol_str;
for (int i=0;i<f1.kol_str;i++)
strcpy(stroka[i],f1.stroka[i]);
FILE *ff=fopen(filnam,"wt");
if (ff!=NULL)
{
for (int i=0;i<kol_str;i++)
fputs(stroka[i],ff);
fclose(ff);
}
else
cout<<"Ошибка. Файл не открыт.";
}
};
void main(void)
{
setlocale(LC_ALL,"Rus");
ffile fil1,fil2;
int choice;
int input_yes=0;
while(1==1)
{
//clrscr();
system("cls");
cout<<"1 - Ввод имени файла"<<endl;
cout<<"2 - Создание нового файла"<<endl;
cout<<"3 - Добавление в конец файла"<<endl;
cout<<"4 - Чтение файла"<<endl;
cout<<"5 - Редактирование файла"<<endl;
cout<<"6 - Копирование файла"<<endl;
cout<<"7 - Выход"<<endl;
cout<<"Ваш выбор->"<<endl;
cin>>choice;
system("cls");
//clrscr();
switch(choice)
{
case 1:
{
cout<<"Ваш выбор: 1 - Ввод имени файла"<<endl;
fil1.vvod_nam();
cout<<"Ввод имени закончен. Press any key"<<endl;
input_yes=1;
_getch();
break;
}
case 2:
{
cout<<"Ваш выбор: 2 - Создание нового файла"<<endl;
if (input_yes==1)
fil1.create_file();
else
cout<<"Не введено имя файла. Вернитесь к пункту 1..."<<endl;
cout<<"Файл успешно записан. Press any key"<<endl;
_getch();
break;
}
case 3:
{
cout<<"Ваш выбор: 3 - Добавление в конец файла"<<endl;
if (input_yes==1)
fil1.append_file();
else
cout<<"Не введено имя файла.Вернитесь к пункту 1..."<<endl;
cout<<"Файл успешно дописан. Press any key"<<endl;
_getch();
break;
}
case 4:
{
cout<<"Ваш выбор: 4 - Чтение файла"<<endl;
if (input_yes==1)
fil1.print_file();
else
cout<<"Не введено имя файла.Вернитесь к пункту 1 ..."<<endl;
cout<<"Вывод файла закончен. Press any key"<<endl;
_getch();
break;
}
case 5:
{
cout<<"Ваш выбор: 5 - Редактирование файла"<<endl;
if (input_yes==1)
fil1.change_file();
else
cout<<"Не введено имя файла. Вернитесь к пункту 1..."<<endl;
cout<<"Файл успешно изменен. Press any key"<<endl;
_getch();
break;
}
case 6:
{
cout<<"Ваш выбор: 6 - Копирование файла"<<endl;
if (input_yes==1)
fil2.copy_file(fil1);
else
cout<<"Не введено имя файла.Вернитесь к пункту 1..."<<endl;
cout<<"Копирование файла успешно завершино. Press any key"<<endl;
_getch();
break;
}
case 7: exit(0); break;
}
}
}
] |