Firstly, you really do need to improve your indentation, e.g.,
Code:
void account_delete(char *filename)
{
int count = 0, i = 0, g, h;
char NAME[20];
account t[20];
FILE *file = fopen(filename, "rb");
if (file != NULL)
{
while (fread(&t[i], sizeof(account), 1, file) == 1)
{
i++;
count++;
}
}
printf("give the name of account to delete");
scanf("%s", &NAME);
for (g = 0; g < count; g++)
{
if (strcmp(t[g].name, NAME) == 0)
{
for (h = g; h < count; h++)
{
t[h] = t[h+1];
}
}
}
count--;
for (i = 0; i < count; i++)
{
//this open the file in "wb" and write to content of t[i] in the file. This one still works with other functions.
account_overwrite(filename, t[i]);
}
fclose(file);
view_account(filename);
}
The comment for account_overwrite() is strange. If account_overwrite() opens the file with the "wb" mode, then each time it is called, it truncates the file to zero length, and then writes the account provided to the file. This could explain why you only have one account left.
Rather, I suggest that you have a store_accounts() function (or something like that) with a signature like this:
Code:
void store_accounts(const char *filename, const account accounts[], size_t count);
Then you can open the file with "wb" mode and write the entire accounts array to the file.
Likewise, instead of having to write the code to retrieve the accounts from file into an array in each function, you can have a retrieve_accounts() function with a signature like this:
Code:
void retrieve_accounts(const char *filename, account accounts[], size_t count);
When implementing this function, be careful to avoid buffer overflow by ensuring that you only read up to count number of accounts. Your current code does not do this. When you are ready to handle dynamic memory allocation and so remove this restriction on array length, you can then change retrieve_accounts() to:
Code:
void retrieve_accounts(const char *filename, account *accounts, size_t *count);
In which case count will contain the number of accounts retrieved, and accounts will be dynamically allocated and so will need to be freed.