Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <string.h>
- struct produse_electronice{
- char prod[20], model[20], categ[20];
- int pret;
- char reteaua[20], culoare[20];
- int g, buc1;
- char tip[20], ram[20];
- int diag, buc2;
- };
- void afisare(struct produse_electronice t[100], int *n) {
- int i;
- for (i = 0; i < *n; i++)
- {
- printf("%s %s %d %s ", t[i].prod, t[i].model, t[i].pret, t[i].categ);
- if (stricmp(t[i].categ, "TELEFOANE") == 0)
- printf("%s %s %d %d\n", t[i].reteaua, t[i].culoare, t[i].g, t[i].buc1);
- else
- printf("%s %s %d %d\n", t[i].tip, t[i].ram, t[i].diag, t[i].buc2);
- }
- }
- int adaugare(struct produse_electronice t[100], int *n) {
- FILE *f;
- if (fopen_s(&f, "stoc.txt", "rt")) {
- printf("Error");
- }
- else
- {
- int sw = 0;
- while (!feof(f))
- {
- sw = 1;
- fscanf(f, "%s", t[*n].prod, _countof(t[*n].prod));
- fscanf(f, "%s", t[*n].model, _countof(t[*n].model));
- fscanf(f, "%d", &t[*n].pret);
- fscanf(f, "%s", t[*n].categ, _countof(t[*n].categ));
- if (stricmp(t[*n].categ, "TELEFOANE") == 0) {
- fscanf(f, "%s", t[*n].reteaua, _countof(t[*n].reteaua));
- fscanf(f, "%s", t[*n].culoare, _countof(t[*n].culoare));
- fscanf(f, "%d", &t[*n].g);
- fscanf(f, "%d", &t[*n].buc1);
- }
- else
- {
- fscanf(f, "%s", t[*n].tip, _countof(t[*n].tip));
- fscanf(f, "%s", t[*n].ram, _countof(t[*n].ram));
- fscanf(f, "%d", &t[*n].diag);
- fscanf(f, "%d", &t[*n].buc2);
- }
- (*n)++;
- }
- if (sw)
- afisare(t, n);
- fclose(f);
- return sw;
- }
- fclose(f);
- }
- void salvare(struct produse_electronice t[100], int *n) {
- FILE *g;
- fopen_s(&g, "stoc.txt", "wt");
- int i;
- for (i = 0; i <= (*n); i++) {
- fprintf(g, "%s %s %d %s ", t[i].prod, t[i].model, t[i].pret, t[i].categ);
- if (stricmp(t[i].categ, "TELEFOANE") == 0)
- {
- fprintf(g, "%s %s %d %d", t[i].reteaua, t[i].culoare, t[i].g, t[i].buc1);
- if (i != (*n))
- fprintf(g, "\n");
- }
- else
- {
- fprintf(g, "%s %s %d %d", t[i].tip, t[i].ram, t[i].diag, t[i].buc2);
- if (i != (*n))
- fprintf(g, "\n");
- }
- }
- (*n)++;
- }
- void introducere(struct produse_electronice t[100], int *n) {
- printf("Producator: "); scanf_s("%s", t[*n].prod, _countof(t[*n].prod));
- printf("Model: "); scanf_s("%s", t[*n].model, _countof(t[*n].model));
- printf("Pret: "); scanf_s("%d", &t[*n].pret);
- printf("Categorie: "); scanf_s("%s", t[*n].categ, _countof(t[*n].categ));
- if (stricmp(t[*n].categ, "TELEFOANE") == 0) {
- printf("Reteaua: "); scanf_s("%s", t[*n].reteaua, _countof(t[*n].reteaua));
- printf("Culoarea: "); scanf_s("%s", t[*n].culoare, _countof(t[*n].culoare));
- printf("Greutate: "); scanf_s("%d", &t[*n].g);
- printf("Bucati: "); scanf_s("%d", &t[*n].buc1);
- }
- else
- {
- printf("Tip: "); scanf_s("%s", t[*n].tip, _countof(t[*n].tip));
- printf("Ram: "); scanf_s("%s", t[*n].ram, _countof(t[*n].ram));
- printf("Diagonala: "); scanf_s("%d", &t[*n].diag);
- printf("Bucati: "); scanf_s("%d", &t[*n].buc2);
- }
- salvare(t, n);
- }
- int main() {
- struct produse_electronice t[100];
- int n = 0;
- int opt;
- do {
- system("cls");
- printf("1.Citire+Afisare\n2.Afisare\n3.Introducere+Salvare\n0.Iesire\n");
- printf("Optiune: "); scanf_s("%d", &opt);
- switch (opt) {
- case 1: if (adaugare(t, &n) == 0) printf("Nu exista elemente in fisier");
- system("pause");
- break;
- case 2: afisare(t, &n); system("pause"); break;
- case 3: introducere(t, &n); break;
- default: break;
- }
- } while (opt != 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement