I have written the code to write all the records in typedef struct CONTACT people
It writes the first file ok but when you add a second entry it prints the 'file write error'which I don't understand as the condition is not equal to 1.
cheers
code below
Code:
/* Save the list. Writes file to disc*/
void save(void)
{
FILE *fp;
int n;
int dex;
int index;
if((fp=fopen("maillist.txt", "wb"))==NULL) /* binary file*/
{
printf("Cannot open file.\n");
return;
}
for(index=0;index<MAX;index ++) /* write spaces with underscorce in file*/
{
for(dex=0; dex < strlen(people[index].address1); dex++)
{
if (people[index].address1[dex]==' ')
people[index].address1[dex]='_';
}
}
for(n=0; n<MAX; n++)
if(*people[n].surname)
if(fwrite(&people[n], sizeof(CONTACT), n, fp)!=1)
printf("File write error.\n");
fclose(fp);
}
Code tags added by moderator