Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct {
- int nr;
- char nume[30];
- int grupa;
- int pp;
- int pt[10];
- int pe;
- int is;
- }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 BinInText(char *numeBin, char *numeTxt, Student s) {
- FILE *f, *g;
- int i;
- f = fopen(numeBin, "rb");
- g = fopen(numeTxt, "w");
- fprintf(g, "Nr. Nume %30s Grupa Pct proba Pct teme %20s Pct examen\n", " ", " ");
- fread(&s, sizeof(Student), 1, f);
- while(!feof(f)) {
- if(s.is == 1) {
- fprintf(g, "%3d %6s %30d %10d", s.nr, s.nume, s.grupa, s.pp);
- for(i = 0; i < 10; i++)
- fprintf(g, " %d ", s.pt[i]);
- fprintf(g, " %d\n", s.pe);
- }
- fread(&s, sizeof(Student), 1, f);
- }
- fclose(f);
- fclose(g);
- }
- int main() {
- Student s;
- FILE *f;
- char nume[20], numetxt[20];
- int nr, i;
- int length;
- printf("Fisier: "); gets(nume); fflush(stdin);
- printf("Fisier txt: "); gets(numetxt); fflush(stdin);
- f = fopen(nume, "wb");
- if(!f) printf("Fisierul nu s-a deschis!");
- else {
- printf("Nr. mat: ");
- scanf("%d", &nr);
- while(!feof(stdin)) {
- if(nr >= nrart(f, sizeof(Student))) {
- fseek(f, 0, 2);
- s.is = 0;
- for(i = nrart(f, sizeof(Student)); i <= nr; i++)
- fwrite(&s, sizeof(Student), 1, f);
- }
- fseek(f, nr*sizeof(Student), 0);
- fread(&s, sizeof(Student), 1, f);
- if(s.is == 1) printf("Duplicat!");
- else {
- s.nr = nr;
- fflush(stdin);
- printf("Nume: "); gets(s.nume);
- length = strlen(s.nume);
- if(s.nume[length-1] == '\n') s.nume[length-1] = '\0';
- printf("Grupa: "); scanf("%d", &s.grupa);
- printf("Pct prb: "); scanf("%d", &s.pp);
- for(i = 0; i < 10; i++) {
- printf("Tema %d: ", i+1);
- scanf("%d", &s.pt[i]);
- }
- printf("Pct exam: "); scanf("%d", &s.pe);
- s.is = 1;
- fseek(f, nr*sizeof(Student), 0);
- fwrite(&s, sizeof(Student), 1, f);
- }
- printf("Nr. mat: "); scanf("%d", &nr);
- }
- fclose(f);
- BinInText(nume, numetxt, s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement