samedi 31 octobre 2015

How to add multiple entries to a file in C?

I am working on basic file streaming in C and the aim is, to create a sort of database, which contains, s.no, name and attendance of a student. The problem I am facing is, when I enter a new entry, into the database; it replaces the previous entry. Whereas I want to do add multiple entries.

Any help is highly appreciated.

Thank You

The Code that i have written till now:

char file_write()

  {  
    char nm[80];
    int a,d,i=0;
    char e[10]="Absent";
    char f[10]="Present";
    FILE *fp;
    fp = fopen("/Users/kartik/Desktop/stdb.txt","w+");

do{

printf("\nEnter Roll Number: ");           //Input Multiple Variable Integer
scanf("%d",&a);
fprintf(fp,"%d",a);


printf("\nEnter Name: ");                  //Input Variable Character
scanf("%s", &nm);
fprintf(fp,"%s", nm);

printf("Attendance ('0' For Present, '1' for absent): ");   //Input Attendance
scanf("%d",&d);

if(d==0)
{
    fprintf(fp,"%s\n",f);
}

else
{

    fprintf(fp,"%s\n",e);
}

} while(i!='\0');

fclose(fp);
return 0;
}

Aucun commentaire:

Enregistrer un commentaire