Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- typedef struct {
- int codp;
- char nume[30];
- char gen[20];
- float pret;
- int cant;
- }CARTI;
- void creare() {
- FILE *f;
- char nume[20];
- CARTI c;
- printf("introduceti numele fisierului:");
- gets(nume);
- fopen_s(&f, nume, "wb");
- if (!f)
- printf("Eroare");
- else {
- printf("Introduceti codul cartii:");
- scanf_s("%d", &c.codp);
- while (!feof(stdin)) {
- getchar();
- printf("Introduceti numele cartii:");
- gets(c.nume);
- printf("Introduceti genul cartii:");
- gets(c.gen);
- printf("Introduceti pretul cartii:");
- scanf_s("%f", &c.pret);
- printf("Introduceti numarul de carti:");
- scanf_s("%d", &c.cant);
- fwrite(&c, sizeof(CARTI), 1, f);
- printf("Introduceti codul cartii:");
- scanf_s("%d", &c.codp);
- }
- fclose(f);
- }
- }
- void cheie_unica() {
- FILE *f;
- char nume[20];
- CARTI c;
- printf("Introduceti numele fisierului:");
- gets(nume);
- fopen_s(&f, nume, "rb+");
- if (!f)
- printf("Eroare");
- else {
- int codp;
- printf("Introduceti codul cartii:");
- scanf_s("%d", &codp);
- while (!feof(stdin)) {
- fseek(f, 0, 0);
- int gasit = 0;
- fread(&c, sizeof(CARTI), 1, f);
- while (!feof(f) && gasit == 0) {
- if (c.codp == codp) {
- printf("%s %s %f %d\n", c.nume, c.gen, c.pret, c.cant);
- c.pret = c.pret + 0.2*c.pret;
- fseek(f, ftell(f) - sizeof(CARTI), 0);
- fwrite(&c, sizeof(CARTI), 1, f);
- gasit = 1;
- }
- else {
- fread(&c, sizeof(CARTI), 1, f);
- }
- }
- if (gasit == 0)
- printf("Cartea nu exista\n");
- printf("Introduceti codul:");
- scanf_s("%d", &codp);
- }
- fclose(f);
- }
- }
- void selectie() {
- FILE *f;
- char nume[20];
- CARTI c;
- printf("Introduceti numele fisierului:");
- gets(nume);
- fopen_s(&f, nume, "rb+");
- if (!f)
- printf("Eroare");
- else {
- FILE *g;
- printf("Introduceti numele fisierului text:");
- gets(nume);
- fopen_s(&g, nume, "w");
- fread(&c, sizeof(CARTI), 1, f);
- while (!feof(f)) {
- if (c.cant > 200)
- {
- fprintf(g, "%d %s %s %f %d\n", c.codp, c.nume, c.gen, c.pret, c.cant);
- c.pret = 0.9*c.pret;
- fseek(f, ftell(f) - sizeof(CARTI), 1, f);
- fwrite(&c, sizeof(CARTI), 1, f);
- fseek(f, 0, 1);
- }
- fread(&c, sizeof(CARTI), 1, f);
- }
- fclose(g);
- fclose(f);
- }
- }
- void raport() {
- FILE *f;
- char nume[20];
- CARTI c;
- printf("Introduceti numele fisierului:");
- gets(nume);
- fopen_s(&f, nume, "rb");
- if (!f)
- printf("Eroare");
- else {
- FILE *h;
- char nume[20];
- printf("Introduceti numele fisierului text : ");
- gets(nume);
- fopen_s(&h, nume, "w");
- fprintf(h, "CodProdus Denumire Gen Pret Cantitate\n");
- fseek(f, 0, 0);
- fread(&c, sizeof(CARTI), 1, f);
- while (!feof(f))
- {
- fprintf(h, "%5d %10s %-5s %-5.2f %-3d\n", c.codp, c.nume, c.gen, c.pret, c.cant);
- fread(&c, sizeof(CARTI), 1, f);
- }
- fclose(h);
- fclose(f);
- }
- }
- void main() {
- creare();
- cheie_unica();
- selectie();
- raport();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement