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);
}