Главная » 2018 » Май » 8 » Файлы. Структуры
08:23
Файлы. Структуры

#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 summa(FILE *file);
int main()
{
    setlocale(LC_ALL, "Russian");
    FILE *f; 
    srand(time(0));
    char s[20]="d:/files/1.txt";
    f=fopen(s,"wb");
    sozdanie(f);
    fclose(f);
    f=fopen(s,"rb");
    cout<<"Содержимое файла: "<<endl;
    print(f);
    fclose(f);
    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)
{
    person dat;
    while (fread(&dat,sizeof(person),1,file))
        cout<<dat.name<< " "<<dat.age<<endl;
}

Просмотров: 326 | Добавил: denjes | Рейтинг: 0.0/0
Всего комментариев: 0
avatar