Главная »
2018 » Май » 8 » Файлы. Выбор режима работы
08:47 Файлы. Выбор режима работы |
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
struct person
{
char name[20];
int age;
};
void sozdanie(FILE *file);
void print(FILE *file);
int main()
{
setlocale(LC_ALL, "Russian");
FILE *f;
char s[20]="d:/files/1.txt";
int n;
do
{
cout<<"Выберите: 1-создание, 2-добавление, 3-вывод, 0-выход -> ";
cin>>n;
switch (n)
{
case 1:
{
f=fopen(s,"wb");
sozdanie(f);
fclose(f);
break;
}
case 2:
{
f=fopen(s,"ab");
sozdanie(f);
fclose(f);
break;
}
case 3:
{
f=fopen(s,"rb");
print(f);
fclose(f);
break;
}
}
} while (n);
system("PAUSE");
}
void sozdanie(FILE *file)
{
person dat;
int n;
cout<<"Добавить нового? 1-YES, 0-NO "; cin>>n;
while (n)
{
cout<<"Введите фамилию ";cin>>dat.name;
cout<<"Введите возраст ";cin>>dat.age;
fwrite(&dat,sizeof(person),1,file);
cout<<"Добавить нового? 1-YES, 0-NO "; cin>>n;
}
}
void print(FILE *file)
{
cout<<"---------------------------------------"<<endl;
person dat;
while (fread(&dat,sizeof(person),1,file))
cout<<dat.name<< " "<<dat.age<<endl;
cout<<"---------------------------------------"<<endl;
}
|
Просмотров: 297 |
Добавил: denjes
| Рейтинг: 0.0/0 |