-1

I have three records in the binary file. When I am modifying a record and I open the text file with readable records,the record with the modified name just duplicates and has nothing changed.Also, sometimes the console says that the record doesn't exist, even if it does.The first 2 subprograms are for creating the binary file and the text file with the records from the binary file.

#include<stdio.h>
typedef struct {
    char CNP[14];// CNP = Personal Identification Number
    char nume[30]; // nume=name
    int an;        // an=year
    int grupa;     // grupa=group
    int nrDisc;    // nrDisc=number of classes           
    int note[20];  // note=grades
}STUDENT;

void creare(char*nume) {       // creare=creation
    FILE*f;
    STUDENT s;
    fopen_s(&f, nume, "wb");
    if (!f)
        printf("Eroare");
    else {
        printf("CNP:");
        gets(s.CNP);
        while (!feof(stdin)){
            printf("Nume:");
            gets(s.nume);
            printf("An:");
            scanf_s("%d", &s.an);
            printf("Grupa:");
            scanf_s("%d", &s.grupa);
            printf("Nr. discipline:");
            scanf_s("%d", &s.nrDisc);
            for (int i = 0; i < s.nrDisc; i++)
            {
                printf("Nota[%d] este:", i);
                scanf_s("%d", &s.note[i]);
            }
            fwrite(&s, sizeof(s), 1, f);
            getchar();
            printf("CNP:");
            gets(s.CNP);
        }
        fclose(f);
    }
}


void raport(char*nume_binar, char*nume_text) {   // raport=report
    FILE*f;
    fopen_s(&f, nume_binar, "rb");
    if (!f)
        printf("Eroare");
    else {
        FILE*g;
        STUDENT s;
        fopen_s(&g, nume_text, "w");
        fprintf(g,"     CNP             Nume           An       Grupa    Nr discipline\n\n");
        while(fread(&s, sizeof(s), 1, f)==1)
         {          
            fprintf(g,"%s   %s  %5d  %9d  %9d\n", s.CNP, s.nume, s.an, s.grupa, s.nrDisc);
            for (int i = 0; i < s.nrDisc; i++)
            {
                fprintf(g, "Nota %d este:", i);
                fprintf(g, "%d\n", s.note[i]);

            }
            fprintf(g, "\n");
        }

        fclose(g);

    }
    fclose(f);
}


void modificare(char*nume) {  // modificare=modifying
    FILE*f;
    STUDENT s;
    int grupa;
    fopen_s(&f, nume, "rb+");
    if (!f)
        printf("Eroare");
    else {
        printf("Grupa:");
        scanf_s("%d", &grupa);
        while (!feof(stdin)) {
            int g=0;
            char nume[20];
            fread(&s, sizeof(s), 1, f);
            if (grupa == s.grupa) {
                g = 1;
                printf("Numele initial este:%s\n", s.nume);
                printf("Noul nume este:");
                getchar();
                gets(nume);
                fseek(f,ftell(f)-sizeof(s), 0);
                fwrite(&s, sizeof(s), 1, f);
            }

            if (g == 0)
                printf("Nu exista\n");

            printf("Grupa:");
            scanf_s("%d", &grupa);


        }
        fclose(f);

    }
}

void main() {
    char nume_fisier[] = "Student.dat";
    char nume_fisier_txt[] = "Raport.txt";
    //creare(nume_fisier);
    //raport(nume_fisier, nume_fisier_txt);
    modificare(nume_fisier);
}
Andreea Elena
  • 135
  • 1
  • 8
  • You could use english names for structure members.Maybe as a comment. I could provide an idea why you have two gets(s.CNP); statements in your else block of creare. Also if I remember correctly the records are being stored properly aren't they? – Nevus Mar 28 '19 at 16:48
  • i wrote the translation for the structure members – Andreea Elena Mar 28 '19 at 16:58
  • 1
    [Why is “while (!feof(file))” always wrong?](https://stackoverflow.com/q/5431941/3545273) strikes again: when you have read the last record, the eof flag is not still on. It will only be active after the first failed `gets`, but you process a full nonexistent record! And please stop using the unsafe and deprecated for decades `gets`! – Serge Ballesta Mar 28 '19 at 17:00
  • Why are you checking stdin for EOF in a function to modify a binary file? Also I believe fseek(f, -sizeof(s), SEEK_CUR) would make it more readable than using 0,1,2. If you would like to have pressing EOF to be only way to exit loop you could use conditional break statement such as if ( == EOF) break; – Nevus Mar 28 '19 at 17:13

1 Answers1

0
while (fread(&s, sizeof(s), 1, f)) {
            int g=0;
            char nume[30]; //size of nume here should be same as that of structure

            if (grupa == s.grupa) {
                g = 1;
                printf("Numele initial este:%s\n", s.nume);
                printf("Noul nume este:");
                getchar();
                gets(nume);
                fseek(f,-sizeof(s), SEEK_CUR)) ; //Using macros helps clarify code
                fwrite(&s, sizeof(s), 1, f);
                break;  //break after modifying content

            }
            if (feof(f))
               break;


Nevus
  • 1,307
  • 1
  • 9
  • 21