Sunteți pe pagina 1din 6

1.

S se scrie programul care creeaz un fiier organizat relativ cu date despre

studenii unei faculti. Datele care se rein despre studeni snt: numele, anul, grupa, numrul de note, notele (maxim 15). Cheia relativ a fiierului este numrul matricol al studentului. Datele se preiau de la tastatur, sfritul introducerii fiind marcat standard.
#include<stdio.h> #define fwriteb(x,f) fwrite(&(x),sizeof(Student),1,(f)) #define freadb(x,f) fread(&(x),sizeof(Student),1,(f)) #include<stdio.h> typedef struct{int nr; char nume[30]; int an; int grupa; int n; int note[15]; }Student; int nrart(FILE *f, int l) {long p; int n; p=ftell(f); fseek(f,0,2); n=ftell(f)/l; fseek(f,p,0); return n;} void main() {FILE *f; char s1[20]; Student x; int n,i; printf("\nFisier: ");gets(s1); f=fopen(s1,"wb+"); printf("\nNr matricol: "); scanf("%d",&n); while(!feof(stdin)) {if(n>=nrart(f,sizeof(Student))) {x.is=0; fseek(f,0,SEEK_END); for(i=nrart(f,sizeof(Student));i<=n;i++) fwriteb(x,f);} fseek(f,n*sizeof(Student),SEEK_SET); freadb(x,f); if(x.is) printf("\nExista deja un student cu acest numar matricol"); else{fseek(f,n*sizeof(Student),SEEK_SET); x.nr=n; printf("Nume: "); fflush(stdin);gets(x.nume); printf("An : "); scanf("%d",&x.an); printf("Grupa:"); scanf("%d",&x.grupa); printf("Nr. note:"); scanf("%d",&x.n); for(i=0;i<x.n;i++) {printf("Nota %d: ",i+1); scanf("%d",&x.note[i]);} x.is=1; fwriteb(x,f);} printf("\nNr matricol: "); scanf("%d",&n);}

fclose(f);}

2. S se scrie programul care listeaz, ntr-un fiier text, sub form de tabel, studenii din fiierul creat la problema de la punctul 1.
#include<stdio.h> #define freadb(x,f) fread(&(x),sizeof(Student),1,(f)) typedef struct{int nr; char nume[30]; int an; int grupa; int n; int note[15]; }Student; void main() {FILE *f,*g; char s1[20]; Student x; int i,j; printf("\nFisier: "); gets(s1); if(!(f=fopen(s1,"rb+")))printf("\nFisierul %s nu poate fi deschis",s1); else{printf("\nFisier text: "); gets(s1); g=fopen(s1,"w"); i=0; fprintf(g,"\n Nrc Nrm Nume si prenume %15s An Grupa Note"," "); freadb(x,f); while(!feof(f)) {if(x.is) {fprintf(g,"\n%4d %4d %-30s %2d %3d ",++i,x.nr,x.nume, x.an,x.grupa); for(j=0;j<x.n;j++) fprintf(g,"%2d ",x.note[j]);} freadb(x,f);} fclose(f);} }

3. S se scrie programul care afieaz datele despre studenii ale cror numere

matricole se introduce de la tastatur. Sfritul introducerii este marcat standard.


#include<stdio.h> #define freadb(x,f) fread(&(x),sizeof(Student),1,(f)) typedef struct{int nr; char nume[30]; int an; int grupa; int n; int note[15]; }Student; int nrart(FILE *f, int l) {long p; int n; p=ftell(f);

fseek(f,0,2); n=ftell(f)/l; fseek(f,p,0); return n;} void main() {FILE *f; char s1[20]; Student x; int n,i; printf("\nFisier: ");gets(s1); if(!(f=fopen(s1,"rb")))printf("\nFisierul %s nu poate fi deschis",s1); else{printf("\nNr.matricol: "); scanf("%d",&n); while(!feof(stdin)) {if(n>=nrart(f,sizeof(Student)))printf("\n Nu exista student cu numarul matricol %d",n); else{fseek(f,n*sizeof(Student),SEEK_SET); freadb(x,f); if(!x.is)printf("\n Nu exista student cu numarul matricol %d",n); else{printf("\n %-30s An: %2d Grupa: %2d Note: ",x.nume, x.an,x.grupa); for(i=0;i<x.n;i++) printf("%2d ",x.note[i]);} } printf("\nNr.matricol: "); scanf("%d",&n);} fclose(f);} }

4. S se scrie programul care listeaz, n fiiere text, situaia studenilor din grupele

ale cror numere se introduc de la tastatur. Sfritul introducerii este marcat standard.
#include<stdio.h> #define freadb(x,f) fread(&(x),sizeof(Student),1,(f)) typedef struct{int nr; char nume[30]; int an; int grupa; int n; int note[15]; }Student;

void main() {FILE *f,*g; char s1[20]; Student x; int n,i,j; printf("\nFisier: ");gets(s1); if(!(f=fopen(s1,"rb")))printf("\nFisierul %s nu poate fi deschis",s1); else{printf("\nNr.grupei: "); scanf("%d",&n); while(!feof(stdin)) {i=0; printf("\nFisier text: ");fflush(stdin);gets(s1); g=fopen(s1,"w"); fprintf(g,"\n Nrc Nrm Nume si prenume %14s An Grupa Note"," "); rewind(f); freadb(x,f); while(!feof(f)) {if(x.is) if(x.grupa==n) {fprintf(g,"\n%4d %4d %-30s %2d %3d ",++i,x.nr,x.nume,x.an, x.grupa); for(j=0;j<x.n;j++) fprintf(g,"%2d ",x.note[j]);} freadb(x,f);} fclose(g); printf("\nAu fost listati %d studenti in fisierul %s\n\n\n", i,s1); printf("\nNr.grupei: "); scanf("%d",&n);}

fclose(f);} }

5. S se scrie programul pentru exmatricularea studenilor ale cror numere

matricole se introduc de la tastatur.


#include<stdio.h> #include<ctype.h> #include<conio.h> #define fwriteb(x,f) fwrite(&(x),sizeof(Student),1,(f)) #define freadb(x,f) fread(&(x),sizeof(Student),1,(f)) typedef struct{int nr; char nume[30]; int an; int grupa; int n; int note[15]; }Student; int nrart(FILE *f, int l) {long p; int n; p=ftell(f); fseek(f,0,2); n=ftell(f)/l; fseek(f,0,p); return n;} void main() {FILE *f; char s1[20]; Student x; int n,i; printf("\nFisier: ");gets(s1); if(!(f=fopen(s1,"rb")))printf("\nFisierul %s nu poate fi deschis",s1); else{printf("\nNr.matricol: "); scanf("%d",&n); while(!feof(stdin)) {if(n>=nrart(f,sizeof(Student)))printf("\n Nu exista student cu numarul matricol %d",n); else{fseek(f,n*sizeof(Student),SEEK_SET); freadb(x,f); if(!x.is)printf("\n Nu exista student cu numarul matricol %d",n); else{printf("\n %-30s An: %2d Grupa: %2d Note: ",x.nume, x.an,x.grupa); for(i=0;i<x.n;i++) printf("%2d ",x.note[i]); printf("\nExmatriculez? (d/n): "); if(toupper(getche())=='D') {fseek(f,n*sizeof(Student),SEEK_SET); x.is=0; fwriteb(x,f);} }

} printf("\nNr.matricol: "); scanf("%d",&n);} fclose(f);} }

Htc amaze