#Include Typedef Struct (Int Cod Char Nume (30) Float Pret Int Cant ) CARTI
#Include Typedef Struct (Int Cod Char Nume (30) Float Pret Int Cant ) CARTI
#include<stdio.h>
typedef struct {int cod;
char nume[30];
float pret;
int cant;} CARTI;
void creare()
{
FILE *f; char nume[20]; CARTI p;
printf("nume fisier:"); gets(nume);
fopen_s(&f, nume, "wb");
if (!f)
printf("eroare");
else {
printf("Introduceti codul:");
scanf_s("%d", &p.cod);
while (!feof(stdin))
{
getchar();
printf("nume="); gets(p.nume);
printf("pret="); scanf_s("%f", &p.pret);
printf("cantitate="); scanf_s("%d", &p.cant);
fwrite(&p, sizeof(CARTI), 1, f);
printf("Introduceti codul:");
scanf_s("%d", &p.cod);
}
fclose(f);
}
2.Sa se scrie intr-un fisier tex,t toate elementele dintr-un fisier binar creat
anterior.
void consultare ()
{
FILE *f; char nume[20]; CARTI p;
printf("nume fisier:"); gets(nume);
fopen_s(&f, nume, "rb");
if (!f)
printf("eroare");
else {
FILE *g;
printf("Introduceti nume nou fisier:"); gets(nume);
fopen_s(&g, nume, "w");
fread(&p, sizeof(CARTI), 1, f);
while (!feof(f))
{
fprintf(g, "%d %s %f %d \n", p.cod, p.nume, p.pret, p.cant);
fread(&p, sizeof(CARTI), 1, f);
}
fclose(f);
fclose(g);
}
}