![]() |
| | #1 |
| Registered User Join Date: May 2008
Posts: 70
| random access and sequential binary files and functions issues 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++)
{
account_overwrite(filename,t[i]);//this open the file in "wb" and write to content of t[i] in the file. This one still works with other functions.
}
fclose(file);
view_account(filename);
}
|
| overlord21 is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| 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);
}
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); 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); Code: void retrieve_accounts(const char *filename, account *accounts, size_t *count);
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #3 |
| Registered User Join Date: May 2008
Posts: 70
| Code: void view_account(char *filename)
{
int count=0;
int i=0;
account t[30];
FILE *file=fopen(filename,"rb");
if(file!=NULL)
{
while(fread(&t[i],sizeof (account),1,file)==1)
{
i++;
count++;
}
}
printf( "Acc.Name | Password| Privilege");
for(i=0;i<count;i++)
{
printf("\n %s | %s | %s\n ",t[i].name,t[i].password,t[i].previleges);
}
}
|
| overlord21 is offline | |
| | #4 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
Code: if (file != NULL)
{
while (fread(&t[i], sizeof(account), 1, file) == 1)
{
i++;
count++;
}
}
Incidentally, fread() can read multiple accounts in one call, so I would expect code like this: Code: size_t retrieve_accounts(const char *filename, account *accounts, size_t capacity)
{
size_t size = 0;
FILE *file = fopen(filename, "rb");
if (file != NULL)
{
size = fread(accounts, sizeof(account), capacity, file);
fclose(file);
}
return size;
}
Code: account accounts[30]; size_t count = retrieve_accounts(filename, accounts, 30);
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
![]() |
| Tags |
| binary, files, programming, random, sequential |
| Thread Tools | |
| Display Modes | |
|