Ich versuche, binäre Datenbank schreiben Programm in C zu erstellen.So erstellen Sie eine Datenbank mit Strukturen in C
Problem ist, dass wenn die Datenbank aktualisiert wird das Programm vorherige Daten der Datei löscht und nur neue aktualisierte Daten speichert.
Programm:
#include <stdio.h>
int main(){
struct STUINFO { char fname[30], lname[30], year[5], batchno[30];};
int id, s, roll;
FILE *outfile;
// open file for writing
if (outfile = fopen ("stuinfo.bin", "w") == NULL)
{
fprintf(stderr, "\nError opening file\n");
return (-1);
}
printf("\nEnter Nine Digit Enrollment no. :\n");
scanf("%d", &roll);
id = roll - 100000000;
if (id < 0) {
printf("Please Enter Valid Nine Digit no.\n");
return -2;
}
struct STUINFO output;
printf("Enter First Name :\n");
scanf("%s", output.fname);
printf("Enter Last Name :\n");
scanf("%s", output.lname);
printf("Enter Year of Semester :\n");
scanf("%s", output.year);
printf("Enter Batch no. :\n");
scanf("%s", output.batchno);
s = sizeof(struct STUINFO);
fseek(outfile, +id*s, SEEK_CUR);
// write struct to file
if(fwrite (&output, sizeof(struct STUINFO), 1, outfile) != 0)
printf("contents to file written successfully !\n");
else
printf("error writing file !\n");
return 0;
}
'include stdio.h' ist ein Syntaxfehler. – melpomene
Der offene Modus 'w' löscht die Datei. Wenn Sie an eine Datei anhängen möchten, verwenden Sie 'a'. – Barmar
BTW 'fwrite! = 0' ist Unsinn – BLUEPIXY