hello again.. this is an update from a post from a few days ago.. ive come a long way but still need help..
in my code below i am reading from a file called transact.txt and which has entries like ''id, name, dollar amount" ie. 1000 joe 100.00
i am trying to use fprint to output the information ive put in an array into a file called accounts.txt. the when i do a printf every line prints. however when i do a fprint only the last line of the file prints.. also i need to figure out how to sort the output by id... any ideas are appreciated..
#define MAXACCTS 16
#define BUF 64
#define NAME_SZ 10
int main()
{
FILE *fp;
FILE *out;
char name[10];
int id[MAXACCTS], i, j, k;
float amount[MAXACCTS];
char buffer[BUF];
//open file for reading
if ((fp = fopen("transact.txt", "r")) == NULL)
{
perror("Unable to open transact.txt\n");
system("pause");
return -1;
}
else
while (fgets(buffer, BUF, fp) !=NULL)
{
if(sscanf(buffer, "%s%s%s", id, name, amount) ==3)
{ k = atoi(id);
if (k > 999 && k <10000)
{
if (strlen(name) < NAME_SZ)
{ out = fopen("accounts.txt", "w+");
if (out == NULL)
{ perror("unable to open accounts.txt\n");
system("pause");
exit (-2);
}
printf("%s\t%s\t\t%.2f\n", id, name, amount);
fprintf(out, "%s\t%s\t\t%.2f\n", id, name, amount);
}
else
printf("Account Name: \"%s\" is over %d characters\n", name, NAME_SZ -1);
}
else
printf("Invalid ID: %s\t%s\t\t%.2f\n",id, name, amount );
}
}
fcloseall();
system("pause");
return 0;
}
![]()
![]()
![]()



LinkBack URL
About LinkBacks



