如何修复正在修改二进制文件中的记录的子程序

时间:2019-03-28 16:35:10

标签: c

我在二进制文件中有3条记录。当我修改记录并打开具有可读记录的文本文件时,具有修改后名称的记录只是重复且没有任何更改。此外,有时控制台会说该记录不存在,即使确实存在。 2个子程序用于创建二进制文件和带有二进制文件中记录的文本文件。

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

1 个答案:

答案 0 :(得分:0)

I used hibernate and jpa to obtain all my DB so it would be faster and easier.
I 


@Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    private String img;
    @NotEmpty
    private String nombre;
    @NotNull
    private Integer precio;
    @NotNull
    private Integer unidades;

    //bi-directional many-to-one association to Detalle
    @JsonIgnore
    @OneToMany(mappedBy="producto")
    private List<Detalle> detalles;

    //bi-directional many-to-one association to Cat
    @JsonIgnore
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="idcat")
    private Cat cat;

    //bi-directional many-to-one association to Tipoconsola
    @JsonIgnore
    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="idconsola")
    private Tipoconsola tipoconsola;

        @Transient
    private Integer idTC;

    @Transient 
    private Integer idCategoria;